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.