delphigoogle-oauthaccess-tokenindybad-request

Delphi Google oauth2 token request using Indy HTTP get 400 Bad Request


I am trying to request access and refresh tokens from Google using oauth2, all I am getting is a "HTTP/1.1 400 Bad Request" error.

I am using IdHTTP in Delphi XE5 with the IdSSLIOHandlerSocketOpenSSL handler. I have got my Client ID, Client Secret and API Key. I have the Authorization Code.

IdHTTP is configured:

AllowCookies = True
HandleRedirects = True
HTTPOptions.hoKeepOrigProtocol = True
HTTPOptions.hoNoProtocolErrorException = True

Everything else is default.

Is there anything I should be doing beforehand, such as authentication?

What is the relevance of the redirect_uri when creating a Win32 application?

All help will be gratefully received.

This is my code:

var  Params: TStringList;
     Resp: TStringStream;
     URI: String;
begin

     Params := TStringList.Create;
     Resp := TStringStream.Create;

     try
        URI := 'https://accounts.google.com/o/oauth2/token';

        Params.Add('client_id=' + clientID);
        Params.Add('client_secret=' + clientSecret);
        Params.Add('code=' + authCode);
        Params.Add('redirect_uri=http://localhost');
        Params.Add('grant_type=authorization_code');

        IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
        IdHTTP.Post(URI, Params, Resp);

        Memo1.Lines.LoadFromStream(Resp);
        Memo1.Lines.Insert(0, IdHTTP.ResponseText);

        finally
        FreeAndNil(Params);
        FreeAndNil(Resp);
        end;

end;

EDIT:

I changed the Accept header to a setting I found elsewhere.
I added charset=utf-8 to Content-Type.
I replaced the clientID and clientSecret.

This is what IdHTTP is sending:

POST /o/oauth2/token HTTP/1.1<br>
Content-Type: application/x-www-form-urlencoded; charset=utf-8<br>
Content-Length: 240<br>
Host: accounts.google.com<br>
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<br>
Accept-Encoding: identity<br>
User-Agent: Mozilla/3.0 (compatible; Indy Library)<br>

client_id=clientID&client_secret=clientSecret&code=4%2FtmTyuvpcSMIkoyJcFmicfXQEDpYiw_QilkO2dXv_nUc&redirect_uri=http%3A%2F%2Flocalhost&grant_type=authorization_code


Solution

  • I solved it!

    Indy was the problem.

    I used synapse 4.0 components instead and had it working within 10 mins.

    That's a day and a half I'll never get back. Thanks Indy :(