用Indy登录facebook

Ben*_*iss 8 delphi post facebook indy

我想和Indy一起登录我的facebook账户.版本是9.00.10,我使用OpenSSL和TIDHTTP并为其分配了一个cookie管理器.一切正常(我可以发送POST请求GET等)

我嗅到实际登录到facebook,我有以下信息:

  • UserAgent:Mozilla/5.0(Windows; U; Windows NT 6.0; en-US; rv:1.9.2)Gecko/20100115 Firefox/3.6(.NET CLR 3.5.30729)

  • 有几个POST参数:

    • lsd =我不知道那是什么.
    • email =实际的Facebook用户名/电子邮件.
    • pass =密码(未加密) - >我很惊讶地看到明文.
    • default_persistent =(0或1)表示"让我登录"
    • timezone =时区代码.
    • lgnrnd =我不知道那是什么.
    • lgnjs =我不知道那是什么.
    • locale = GEOIP位置(ex en_US)

该帖子是在https://www.facebook.com/login.php?login_attempt=1上发布的.但是当我尝试登录时返回我输入的电子邮件不正确.我确定我使用了正确的电子邮件和密码.

这是我的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  TEST : STRING;
  lParamList: TStringList;
  i : Integer;
begin
  lParamList := TStringList.Create;
  lparamlist.Add('lsd=AVoBzJ5G');
  lparamlist.Add('email=myeMail%40mysite.com');
  lparamlist.Add('pass=mypass');
  lparamlist.Add('default_persistent=0');
  lparamlist.Add('timezone=240');
  lparamlist.Add('lgnrnd=210302_FeQV');
  lparamlist.Add('lgnjs=1367035381');
  lparamlist.Add('locale=en_US');
  IDHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
  Test := IdHTTP1.Get('https://www.facebook.com'); // To get the first cookies.
  for i := 0 to IDHTTP1.CookieManager.CookieCollection.Count - 1 do begin
    ShowMessage(IDHTTP1.CookieManager.CookieCollection.Items[i].CookieText); // Show me the cookies.
  end;
  TEST := IDHTTP1.Post('https://www.facebook.com/login.php?login_attempt=1', lParamList);
  StrToFile ('text.html', test);
  ShellExecute (0, 'open', 'text.html', '', '', SW_SHOW);
end;
Run Code Online (Sandbox Code Playgroud)

我使用了从LiveHTTPHeaders获得的参数.如何使用Indy成功登录Facebook?

编辑:尝试使用XE2和Indy 10,但我收到'错误的电子邮件'错误:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdCookieManager, IdIOHandler,
  IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent,
  IdComponent, DateUtils, ShellAPI, IdTCPConnection, IdTCPClient, IdHTTP, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    IdCookieManager1: TIdCookieManager;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetBetween (Str: String; StrStart : String; StrEnd : String) : String;
var
  iPos    : Integer;
  BackUp  : String;
begin
  result := '';
  iPos := Pos (StrStart, Str);
  if iPos <> 0 then begin
    Delete (Str, 1, iPos + Length (StrStart) - 1);
    iPos := Pos (StrEnd, Str);
    if iPos <> 0 then begin
      result := Copy(Str,1, iPos - 1);
    end;
  end;
end;

function StrToFile(szFilePath:string; dwPosition:DWORD; szInput:string):Boolean;
var
  hFile:      DWORD;
  dwSize:     DWORD;
  dwWritten:  DWORD;
begin
  Result := FALSE;
  hFile := CreateFileW(PWideChar(szFilePath), GENERIC_WRITE, 0, nil, CREATE_ALWAYS, 0, 0);
  if hFile <> INVALID_HANDLE_VALUE then
  begin
    dwSize := Length(szInput) * 2;
    if dwSize > 0 then
    begin
      SetFilePointer(hFile, dwPosition, nil, FILE_BEGIN);
      WriteFile(hFile, szInput[1], dwSize, dwWritten, nil);
      if dwWritten = dwSize then
        Result := TRUE;
    end;
    CloseHandle(hFile);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Response      : String;
  lparamList    : TStringList;
begin
  IDHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
  try
    Response := IDHTTP1.Get('https://www.facebook.com/');
  except

  end;
  lParamList := TStringList.Create;
  lParamList.Add('lsd='+GetBetween (Response, 'name="lsd" value="', '"'));
  lParamList.Add('eMail=myEmail@mySite.com');
  lParamList.Add('pass=myPassword');
  lParamList.Add('default_persistent'+GetBetween (Response, 'name="default_persistent" value="', '"'));
  lParamList.Add('timezone=240');
  lParamList.Add('lgnrnd='+GetBetween (Response, 'name="lgnrnd" value="', '"'));
  lParamList.Add('lgnjs='+inttostr(DateTimeToUnix(Now)));
  lParamList.Add('locale=en_US');
  IDHTTP1.Request.Referer := 'https://www.facebook.com/';
  try
    Response := IDHTTP1.Post('https://www.facebook.com/login.php?login_attempt=1', lparamList);
  except

  end;
  StrToFile ('test.html', 0, Response);
  ShellExecute (0, 'open', 'test.html', '', '', SW_SHOW);
end;

end.
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 5

如果hoForceEncodeParamsTIdHTTP.HTTPOptions属性中启用了该标志(默认情况下是这样),那么您需要TStringList使用编码的值填充已发布的内容. TIdHTTP.Post()然后将在传输时为您编码值.

假设该hoForceEncodeParams标志被启用,lparamlist.Add('email=myeMail%40mysite.com');将被传输,email=myeMail%2540mysite.com因为该%字符被编码为%25.Facebook将其解码email=myeMail%40mysite.com为无效电子邮件并将其拒绝.

你可以:

  1. 禁用该hoForceEncodeParams标志,以便按TStringList原样传输值.然后,您将负责手动编码.

  2. 离开hoForceEncodeParams启用的标志,并更改lparamlist.Add('email=myeMail%40mysite.com');lparamlist.Add('email=myeMail@mysite.com');代替. 因为Indy 9不对字符进行编码,TIdHTTP.Post()因此在Indy 9中将传输它.取决于Facebook的宽松程度,这可能有效,也可能无效.email=myeMail@mysite.com@

如果升级到Indy 10,TIdHTTP.Post()将在启用标志时对@字符进行编码.%40hoForceEncodeParams