Ouerghi Yassine
Ouerghi Yassine

Reputation: 1887

Connect to Gmail using Indy

I'm trying to log in to Gmail (not the email) through Indy component using Delphi XE5, Using this function:

procedure TForm1.Button1Click(Sender: TObject);
var
  http : TIdHTTP;
  S, GALX, Email, Pass : String;
  lParam : TStringList;

begin
  try
    lParam := TStringList.Create;
    try
      http := TIdHTTP.Create(nil);
      http.IOHandler := IOHandler;
      http.CookieManager := Cookie;
      http.AllowCookies := true;
      http.HandleRedirects := true;
      http.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0';
      http.Request.Host := 'accounts.google.com';
      http.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
      http.Request.ContentType := 'application/x-www-form-urlencoded';

      S := http.Get('https://accounts.google.com/ServiceLogin');

      Delete(S, 1, Pos('GALX', S));
      S := Copy(S, 1, Pos('">', S) - 1);
      Delete(S, 1, Pos('value=', S) + length('value='));

      GALX := S;

      lParam.Add('GALX='+GALX);
      lParam.Add('Email='+Email);
      lParam.Add('Passwd='+Pass);

      Memo1.Lines.Add(http.Post('http://accounts.google.com/ServiceLoginAuth', lParam));
    finally
      http.Free;
    end;
  finally
    lParam.Free;
  end;
end;

Now whenever i try to execute that i get:HTTP/1.0.405 Method Not Allowed. and i only get this error when the email/pass are right, when the email/pass is wrong i get the usual error page, so i'm guessing it's not the POST Method that is not allowed.

What am i doing wrong here?

Upvotes: 2

Views: 505

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596527

You are not submitting all of the input fields that /ServiceLoginAuth looks for. If you look at the HTML for /ServiceLogin, there are 8 other fields posted to /ServiceLoginAuth besides the 3 that you are already sending. When submitting data from an HTML form, you have to submit everything the HTML form wants to submit, you can't just pick and choose what you want. Try adding the other fields and see what happens.

You need to provide the /ServiceLogin URL in the TIdHTTP.Request.Referer property when posting to /ServiceLoginAuth so it thinks that the request is coming from /ServiceLogin.

You are retrieving /ServiceLogin using HTTPS, but you are posting to /ServiceLoginAuth using HTTP instead. You need to use HTTPS.

When the user has multiple Google accounts, /ServiceLogin posts to /AccountChooser, which then redirects back to /ServiceLogin with additional input parameters, so you might need to take that into account as well.

Posting to /ServiceLoginAuth redirects to /CheckCookie, which then redirects to /ManageAccount, so make sure those requests are complete and accurate at each step.

Upvotes: 6

Related Questions