使用Indy连接到Gmail

时间:2014-03-14 00:00:09

标签: delphi gmail indy10

我正在尝试使用Delphi XE5通过Indy组件登录Gmail(而不是电子邮件), 使用此功能:

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;

现在每当我尝试执行时,我得到:HTTP/1.0.405 Method Not Allowed. 当电子邮件/传递正确时,我只收到此错误,当电子邮件/传递错误时,我得到通常的错误页面,所以我猜测不是POST方法是不允许的。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:6)

您没有提交/ServiceLoginAuth寻找的所有输入字段。如果您查看/ServiceLogin的HTML,除了您已发送的3之外,还有8个其他字段已发布到/ServiceLoginAuth。从HTML表单提交数据时,您必须提交HTML表单要提交的所有内容,您不能只选择您想要的内容。尝试添加其他字段,看看会发生什么。

发布到/ServiceLogin时,您需要在TIdHTTP.Request.Referer媒体资源中提供/ServiceLoginAuth网址,以便其认为该请求来自/ServiceLogin

您正在使用HTTPS检索/ServiceLogin,但您正在使用HTTP发布到/ServiceLoginAuth。您需要使用HTTPS。

当用户拥有多个Google帐户时,/ServiceLogin会向/AccountChooser发帖,然后使用其他输入参数重定向回/ServiceLogin,因此您可能需要考虑这一点

发布到/ServiceLoginAuth重定向到/CheckCookie,然后重定向到/ManageAccount,因此请确保这些请求在每一步都是完整准确的。