Google OAuth with Indy in Delphi

I am using Indy10 with Delphi and trying to get Google OAuth to work. The first step is to request the OAuthGetRequestToken method. The code below returns error 400. Any help would be greatly appreciated.

procedure TForm1.Button1Click(Sender: TObject);
    var
      IdHTTP: TIdHTTP;
      IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocketOpenSSL;
      Params: TStringList;
      mString: String;
    begin
      Params := tstringlist.create;
      IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil);
      IdHTTP := TIdHTTP.create(nil);

      with IdSSLIOHandlerSocket1 do begin
        SSLOptions.Method := sslvSSLv3;
        SSLOptions.Mode :=  sslmUnassigned;
        SSLOptions.VerifyMode := [];
        SSLOptions.VerifyDepth := 2;
      end;
      with IdHTTP do begin
        IOHandler := IdSSLIOHandlerSocket1;
        ReadTimeout := 0;
        AllowCookies := True;
        ProxyParams.BasicAuthentication := False;
        ProxyParams.ProxyPort := 0;
        Request.ContentLength := -1;
        Request.ContentRangeEnd := 0;
        Request.ContentRangeStart := 0;
        Request.ContentType := 'application/x-www-form-urlencoded';
        request.host := 'https://www.google.com';
        Request.Accept := 'text/html, */*';

        Request.BasicAuthentication := False;
        Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
        HTTPOptions := [hoForceEncodeParams];
      end;
      Params.Add('scope=https://www.google.com/analytics/feeds/');
      Params.Add('oauth_consumer_key=anonymous');
      Params.Add('oauth_signature_method=HMAC-SHA1');
      Params.Add('oauth_signature=wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D');
      Params.Add('oauth_timestamp=137131200');
      Params.Add('oauth_nonce=4572616e48616d6d65724c61686176');
      showmessage(HTTPDecode(IdHTTP.Post('https://www.google.com/accounts/OAuthGetRequestToken',Params)));

    end;
+3
source share
3 answers

Look at the DataString answer (where the exception is thrown), this will give you a more detailed reason for the problem.
For example, when trying with your code, I first got the "timestamp too far from the current time", then after updating oauth_timestamp I got "Invalid Signature" ...

+1
source

, , 400. - - , .

, OpenSSL, Indy , .

+1

Try routing the request through HTTP Fiddler .

I found this to be a great tool for tracking errors at the http / https level.

- Jeroen

+1
source

Source: https://habr.com/ru/post/1720144/


All Articles