如何在delphi10中使用IdHTTP登录Instagram网站

如何在delphi10中使用IdHTTP登录Instagram网站,delphi,login,idhttp,delphi-10.1-berlin,Delphi,Login,Idhttp,Delphi 10.1 Berlin,我正在尝试使用Delphi中的idhttp登录instagram网站。 我使用谷歌chrome开发者工具来获取数据,这就是我迄今为止所尝试的 procedure TForm1.Button1Click(Sender: TObject); var lHTTP: TIdHTTP; S,M : TStrings; IdSSL: TIdSSLIOHandlerSocketOpenSSL; begin S := TStringList.Create; S.Add('username :

我正在尝试使用Delphi中的idhttp登录instagram网站。 我使用谷歌chrome开发者工具来获取数据,这就是我迄今为止所尝试的

procedure TForm1.Button1Click(Sender: TObject);
var
  lHTTP: TIdHTTP;
  S,M : TStrings;
  IdSSL: TIdSSLIOHandlerSocketOpenSSL;
begin
  S := TStringList.Create;
  S.Add('username :' +Edit1.Text);
  S.Add('password :'+ Edit2.Text);
  lHTTP := TIdHTTP.Create(nil);
  lHTTP.ReadTimeout := 30000;
  lHTTP.HandleRedirects := True;
  IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
  IdSSL.SSLOptions.Method := sslvTLSv1;
  IdSSL.SSLOptions.Mode := sslmClient;
  lHTTP.IOHandler := IdSSL;
  lHTTP.Request.CustomHeaders.Add('x-csrftoken:'+ Edit3.Text);
  lHTTP.Request.CustomHeaders.Add('x-instagram-ajax:1');
  lHTTP.Request.CustomHeaders.Add(' x-requested-with:XMLHttpRequest');
  lHTTP.Request.CustomHeaders.Add('referer:https://www.instagram.com/');
  lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
  lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
  lHTTP.Post('https://www.instagram.com/accounts/login/ajax/', S);
  Memo1.Lines.Add(lHTTP.Request.ToString);
end;
我现在手动获取访问令牌,只是为了测试代码。我被禁止使用HTTP/1.1403。

更新

这是cookies信息

Response Headers
set-cookie:csrftoken=e3YbQ1FobxgMGcLHRAkj****ML97psae; expires=Tue, 20-Jun-2017 19:09:21 GMT; Max-Age=31449600; Path=/
set-cookie:s_network=; expires=Tue, 21-Jun-2016 20:09:21 GMT; Max-Age=3600; Path=/
set-cookie:sessionid=IGSC3074cea6684a55981cc30d3c5222ed9e4675db0aa4665d3f5b7ed4ae09be01b6%3AugN5uNRztrtVarx6LuheBkv5tNuaVHrL%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A348733484%2C%22_token%22%3A%22348733484%3A6dXogo2jTCkOf29JEUxHavzxqp9iUOr4%3Aa4f855cabbd5c5d2999538a8ec9093c6a59af65e7306998a5647341bdd691158%22%2C%22asns%22%3A%7B%225.37.149.220%22%3A28885%2C%22time%22%3A1466536160%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1466536160.338314%2C%22_platform%22%3A4%2C%22_auth_user_hash%22%3A%22%22%7D; expires=Mon, 19-Sep-2016 19:09:21 GMT; httponly; Max-Age=7776000; Path=/
set-cookie:ds_user_id=348733***; expires=Mon, 19-Sep-2016 19:09:21 GMT; Max-Age=


Request Headers
set-cookie:sessionid=IGSC3074cea6684a55981cc30d3c5222ed9e4675db0aa4665d3f5b7ed4ae09be01b6%3AugN5uNRztrtVarx6LuheBkv5tNuaVHrL%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A348***484%2C%22_token%22%3A%22348733484%3A6dXogo2jTCkOf29JEUxHavzxqp9iUOr4%3Aa4f855cabbd5c5d2999538a8ec9093c6a59af65e7306998a5647341bdd691158%22%2C%22asns%22%3A%7B%225.37.149.220%22%3A28885%2C%22time%22%3A1466536160%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1466536160.338314%2C%22_platform%22%3A4%2C%22_auth_user_hash%22%3A%22%22%7D; expires=Mon, 19-Sep-2016 19:09:21 GMT; httponly; Max-Age=7776000; Path=/
set-cookie:ds_user_id=348733***; expires=Mon, 19-Sep-2016 19:09:21 GMT; Max-Age=7776000; Path=/

您没有正确填充
TStringList
。您需要使用
=
而不是
作为
name=value
分隔符。与
CustomHeaders.Add()相同。在后一种情况下,我建议使用
CustomHeaders.Values[]

您还应该使用
Request.Referer
属性,而不是
CustomHeaders.Add()

在提交实际登录之前,通常需要首先请求包含登录表单的HTML页面,以便捕获与HTML关联的任何cookie并随登录表单一起提交。Instagram也不例外

另外,
Request.ToString()
是没有意义的,您根本不应该使用它

最后,您正在泄漏您创建的对象

话虽如此,请尝试以下方法:

procedure TForm1.Button1Click(Sender: TObject);
var
  lHTTP: TIdHTTP;
  IdSSL: TIdSSLIOHandlerSocketOpenSSL;
  Params : TStrings;
  Reply, Token: string;
  Cookie: TIdCookie;
begin
  Params := TStringList.Create;
  try
    Params.Add('username=' + Edit1.Text);
    Params.Add('password=' + Edit2.Text);

    lHTTP := TIdHTTP.Create(nil);
    try
      IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
      IdSSL.SSLOptions.Method := sslvTLSv1;
      IdSSL.SSLOptions.Mode := sslmClient;
      lHTTP.IOHandler := IdSSL;

      lHTTP.ReadTimeout := 30000;
      lHTTP.HandleRedirects := True;

      // capture cookies first...
      // passing nil to ignore any response body data and not waste memory for it...
      lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
      lHTTP.Get('https://www.instagram.com', TStream(nil));

      Cookie := lHTTP.CookieManager.CookieCollection.Cookie['csrftoken', 'www.instagram.com'];
      if Cookie <> nil then
        Token := Cookie.Value;

      // now submit the login webform...
      lHTTP.Request.CustomHeaders.Values['X-CSRFToken'] := Token;
      lHTTP.Request.CustomHeaders.Values['X-Instagram-AJAX'] := '1';
      lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
      lHTTP.Request.Referer := 'https://www.instagram.com/';
      lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
      lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
      Reply := lHTTP.Post('https://www.instagram.com/accounts/login/ajax/', Params);

      Memo1.Text := Reply;
    finally
      lHTTP.Free;
    end;
  finally
    Params.Free;
  end;
end;
procedure TForm1.按钮1点击(发送方:TObject);
变量
lHTTP:TIdHTTP;
IdSSL:TIdSSLIOHandlerSocketOpenSSL;
参数:t字符串;
回复,令牌:string;
曲奇:小甜饼;
开始
Params:=TStringList.Create;
尝试
Params.Add('username='+Edit1.Text);
Params.Add('password='+Edit2.Text);
lHTTP:=TIdHTTP.Create(nil);
尝试
IdSSL:=TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method:=sslvTLSv1;
IdSSL.SSLOptions.Mode:=sslmClient;
lHTTP.IOHandler:=IdSSL;
lHTTP.ReadTimeout:=30000;
lHTTP.HandleRedirects:=True;
//首先捕获cookies。。。
//传递nil以忽略任何响应体数据,并且不会为此浪费内存。。。
lHTTP.Request.UserAgent:=“Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,像Gecko)Chrome/46.0.2490.76 Mobile Safari/537.36”;
lHTTP.Get('https://www.instagram.com",湾仔(无);;
Cookie:=lHTTP.CookieManager.CookieCollection.Cookie['csrftoken','www.instagram.com'];
如果是零,那么
令牌:=Cookie.Value;
//现在提交登录Web表单。。。
lHTTP.Request.CustomHeaders.Values['X-CSRFToken']:=Token;
lHTTP.Request.CustomHeaders.Values['X-Instagram-AJAX']:=“1”;
lHTTP.Request.CustomHeaders.Values['X-Requested-With']:='XMLHttpRequest';
lHTTP.Request.Referer:='https://www.instagram.com/';
lHTTP.Request.ContentType:=“application/x-www-form-urlencoded”;
lHTTP.Request.UserAgent:=“Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,像Gecko)Chrome/46.0.2490.76 Mobile Safari/537.36”;
回复:=lHTTP.Post('https://www.instagram.com/accounts/login/ajax/,参数);
备忘录1.文本:=回复;
最后
lHTTP.Free;
结束;
最后
自由参数;
结束;
结束;
下面是两个请求1的结果HTTP对话框:

1:我没有Instagram帐户,这就是为什么在本例中登录没有经过身份验证。但正如您在下面看到的,代码仍然能够从登录帖子接收带有JSON数据的HTTP 200响应

GET / HTTP/1.1 Host: www.instagram.com Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36 GET/HTTP/1.1 主持人:www.instagram.com 接受:text/html、application/xhtml+xml、application/xml;q=0.9,*/*;q=0.8 用户代理:Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/46.0.2490.76 Mobile Safari/537.36 HTTP/1.1200ok 严格的运输安全:最大年龄=86400 内容语言:英语 设置Cookie:sessionid=;expires=1970年1月1日星期四格林威治标准时间00:00:00;最大年龄=0;路径=/;HttpOnly;Domain=instagram.com 过期时间:2000年1月1日星期六00:00:00 GMT 变化:Cookie、接受语言、接受编码 Pragma:没有缓存 缓存控制:私有、无缓存、无存储,必须重新验证 日期:2016年6月21日星期二20:06:56 GMT X-Frame-Options:SAMEORIGIN 内容类型:text/html 设置Cookie:csrftoken=;expires=2017年6月20日星期二20:06:56 GMT;最大年龄=31449600;路径=/ 设置Cookie:mid=;expires=Mon,2036年6月16日20:06:56 GMT;最大年龄=630720000;路径=/ 连接:保持活力 内容长度: POST/accounts/login/ajax/HTTP/1.0 内容类型:application/x-www-form-urlencoded 内容长度: X-CSRFToken: X-Instagram-AJAX:1 X-request-With:XMLHttpRequest 主持人:www.instagram.com 接受:text/html、application/xhtml+xml、application/xml;q=0.9,*/*;q=0.8 推荐人:https://www.instagram.com/ 用户代理:Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/46.0.2490.76 Mobile Safari/537.36 Cookie:mid=;csrftoken= 用户名=&密码= HTTP/1.1200ok 严格的运输安全:最大年龄=86400 内容语言:英语 过期时间:2000年1月1日星期六00:00:00 GMT 改变:Cookie,接受语言 Pragma:没有缓存 缓存控制:私有、无缓存、无存储,必须重新验证 日期:2016年6月21日星期二20:06:57 GMT 内容类型:application/json 设置Cookie:csrftoken=;expires=2017年6月20日星期二20:06:57 GMT;最大年龄=31449600;路径=/ 连接:关闭 内容长度: {“状态”:“确定”,“已验证”:错误,“用户”:“”}
非常感谢您提供的信息,但是我仍然收到403错误,我想可能是因为除了访问令牌之外还有cookies数据,所以我用cookies更新了帖子。@ColdZer0:您是说
post()
没有发送所有
get()
收到的cookies吗?@ColdZer0:我刚刚测试了我在回答中显示的代码。我能够从Instagram登录中获得包含JSON数据的有效HTTP 200响应,即使使用了无效凭据。我没有收到HTTP 403错误。我使用get请求收到HTTP 200响应,但当我尝试使用instagram帐户时,我收到403错误@ColdZer0:你试过我提供的更新代码了吗?正如我所说,我不能 HTTP/1.1 200 OK Strict-Transport-Security: max-age=86400 Content-Language: en Set-Cookie: sessionid=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/; HttpOnly; Domain=instagram.com Expires: Sat, 01 Jan 2000 00:00:00 GMT Vary: Cookie, Accept-Language, Accept-Encoding Pragma: no-cache Cache-Control: private, no-cache, no-store, must-revalidate Date: Tue, 21 Jun 2016 20:06:56 GMT X-Frame-Options: SAMEORIGIN Content-Type: text/html Set-Cookie: csrftoken=<token>; expires=Tue, 20-Jun-2017 20:06:56 GMT; Max-Age=31449600; Path=/ Set-Cookie: mid=<value>; expires=Mon, 16-Jun-2036 20:06:56 GMT; Max-Age=630720000; Path=/ Connection: keep-alive Content-Length: <HTML byte length> <actual HTML bytes> POST /accounts/login/ajax/ HTTP/1.0 Content-Type: application/x-www-form-urlencoded Content-Length: <params byte length> X-CSRFToken: <token> X-Instagram-AJAX: 1 X-Requested-With: XMLHttpRequest Host: www.instagram.com Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Referer: https://www.instagram.com/ User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36 Cookie: mid=<value>; csrftoken=<token> username=<user>&password=<pass> HTTP/1.1 200 OK Strict-Transport-Security: max-age=86400 Content-Language: en Expires: Sat, 01 Jan 2000 00:00:00 GMT Vary: Cookie, Accept-Language Pragma: no-cache Cache-Control: private, no-cache, no-store, must-revalidate Date: Tue, 21 Jun 2016 20:06:57 GMT Content-Type: application/json Set-Cookie: csrftoken=<token>; expires=Tue, 20-Jun-2017 20:06:57 GMT; Max-Age=31449600; Path=/ Connection: close Content-Length: <json byte length> {"status": "ok", "authenticated": false, "user": "<user>"}