I have the following code:

const
  AGENT = 'User Agent';
  SERVER = 'www.daniweb.com';
  RESOURCE = 'api/access_token';
  ID = ''; // my client id
  SECRET = ''; // my client secret
  CODE = ''; // my returned code, pasted from the browser
  REDIRECT_URI = ''; // my redirect uri
  ACCEPT: packed array [0..1] of LPWSTR = (PChar('*/*'), nil);
  HEADER = 'Content-Type: application/x-www-form-urlencoded';
  DATA: UTF8String = 'code=' + CODE + '&client_id=' + ID + '&client_secret=' + SECRET + '&redirect_uri=' + REDIRECT_URI;

function GetUrl: string;
var
  hHttp, hInet, hRequest: HINTERNET;
  i, bufferLen: Cardinal;
  buffer: array [0..1023] of AnsiChar;
begin
  hInet := InternetOpen(PChar(AGENT), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);

  hHttp := InternetConnect(hInet, PChar(SERVER), INTERNET_DEFAULT_HTTP_PORT,
    nil, nil, INTERNET_SERVICE_HTTP, 0, 1);

  hRequest := HttpOpenRequest(hHttp, PChar('POST'), PChar(RESOURCE), nil, nil,
    @ACCEPT, 0, 1);

  HttpSendRequest(hRequest, PChar(HEADER), Length(HEADER), PWideChar(data), Length(data));

  repeat
    InternetReadFile(hRequest, @buffer, SizeOf(buffer), bufferLen);

    if bufferLen = SizeOf(buffer) then
      result := result + buffer
    else if bufferLen > 0 then
      for i := 0 to bufferLen - 1 do
        result := result + buffer[i];

  until bufferLen = 0;

  InternetCloseHandle(hRequest);
  InternetCloseHandle(hHttp);
  InternetCloseHandle(hInet);
end;

When I post this to a PHP script of my own I can see the POSTed values, but whenever I try to get an access token it fails ("There was an error fetching the access token").

Is there something wrong with the above, something obvious that I'm missing?

If someone has example code in C, C++ or C# using this method, that would be fine too.

The code parameter is based off of your client_id AND redirect_uri so be sure the same redirect_uri is used to fetch the code as is being passed into the subsequent request to fetch the access token.

It looks here as if you're fetching the code parameter from a browser request and then passing in http://dwapi.pritaeas.net/ as the redirect_uri to generate the access_token. Be sure you're passing that same redirect_uri into the browser request used to generate the code.

Closed as logic error. The code works fine. Hope to post a complete code snippet soon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.