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

2023-12-12

我正在尝试在 Delphi 中使用 idhttp 登录 Instagram 网站。 我使用谷歌浏览器开发者工具来获取数据,这是我到目前为止所尝试过的。

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.1 403 Forbidden. !

UPDATE

这是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正确。你需要使用=代替: as the name=value分隔符。与相同CustomHeaders.Add()。在后一种情况下,我建议使用CustomHeaders.Values[]反而。

您还应该使用Request.Referer财产而不是CustomHeaders.Add().

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

Also, 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;

Here is the resulting HTTP dialog for the two requests 1:

1: I don't have an Instagram account, which is why the login is not authenticated in this example. But as you can see below, the code is still able to receive an HTTP 200 response with JSON data from the login POST.



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
  


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>"}
  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在delphi10中使用IdHTTP登录Instagram网站 的相关文章

随机推荐

  • 如何定义由其他数据类型组成的数据类型?

    我想定义数据类型Currency 它由其他三种数据类型组成 我遇到的问题是 Haskell 无法将数据类型识别为货币的一部分 这会破坏一切 我的想法是将不同的货币定义为它们自己的数据类型 然后将它们添加到货币类型中 我尝试过 data Eu
  • RecyclerView 中的选定项目在滚动时发生变化

    我有一个 RecyclerView 其中每个元素代表一个事件 我想让用户通过单击来选择事件 选择后 事件和报告按钮将被着色 执行单击之前的 UI 点击这里 UI 执行单击后 点击这里 这非常简单并且据称有效 我设置了一个OnClickLis
  • Firebase 与 Swift 3 计算儿童数量

    我有一个字符串数组 我正在尝试通过 firebase 填充它 它是一个聊天应用程序 当用户创建房间时 他或她会为该房间命名 当用户登录并进入登陆页面时 它会查询他或她正在参与的所有房间 我希望它填充表格视图 在 firebase 文档中 我
  • javascript onclick create(element) div viz 弹出框

    我正在尝试制作一个弹出框 单击按钮即可调用该弹出框 这就是我到目前为止所得到的 http jsfiddle net WGPhG 2 这是一个真正可以做你想做的事情的小提琴 http jsfiddle net WGPhG 6 JS funct
  • PrimeFaces 验证器未触发

    由于 fileLimit 不再存在于 primefaces 3 4 中 我正在尝试解决实现验证器的问题 问题是方法 validate 从未被调用 这是我的验证器 FacesValidator value fileLimitValidator
  • 需要帮助使用 C++ 在 Linux 环境中的现有应用程序中生成击键

    我需要帮助在 LINUX 环境中的现有应用程序中生成击键 我完全知道 Dev C 中有一些库可以完全满足我的需求 但在 Windows 中 我需要在 Linux 中实现类似的功能 我用谷歌搜索了很多 但找不到任何解决方案 下面是 Dev C
  • .htaccess 重写 url 并删除 .php

    我想通过 htaccess 文件从 url 中删除 php 例如home php to home我在 htaccess 文件中使用以下重写规则 RewriteRule 1 php L 我还想重写网址 例如 detail php ca 38
  • C++为类成员函数分配一个lambda函数以提高计算效率[重复]

    这个问题在这里已经有答案了 更新 改写 我希望通过将类成员函数的运行时分配给以其他类成员为条件的许多函数之一来提高代码的计算效率 一种推荐的解决方案使用 include
  • 如何正确地将 Angular 2 (npm) 升级到最新版本?

    最近我开始了 Angular 2 教程https angular io docs ts latest tutorial 并停止了 Angular 2 beta 8 现在我恢复了教程 最新的测试版是测试版 14 如果我只是这样做npm 更新一
  • C# 中的 SafeFileHandle 是什么?何时应该使用它?

    当我还在学习 System IO 时 File Streamclass 的构造函数 我发现存在类型名为的重载构造函数SafeFileHandle 我尝试在互联网和MSDN文档上搜索 但我什么都看不懂 我发现even stranger这样的词
  • Android 小部件在 x 时间后更新

    我刚刚开始学习 Android 我正在尝试编写一个小部件 它每秒更新一次计数器 但在某个地方它缺少一些东西 我在屏幕上看到 1 我正在使用 Android 4 模拟器 package com ibluekey import java tex
  • 扩展 WP_List_Table / 处理插件管理中的复选框选项

    我正在开发一个 WordPress 插件 该插件的一部分需要扩展 WP List Table 并将该表中签入的任何项目存储到选项中 我已经设法弄清楚如何正确设置和显示所需的表格 但如何处理存储选中的选项 这是我到目前为止所得到的 class
  • 用于验证 dd-Mmm-yyyy 格式日期的正则表达式

    我有一个需要验证的文本输入框 用户应该只能输入 dd Mmm yyyy 格式的日期 例如 2013 年 6 月 1 日 2015 年 8 月 31 日等 或者他们应该能够输入 T 1 T 2 T 99 我可以使用什么样的正则表达式模式来验证
  • Cypher zip 集合

    如果我有两个集合 我如何将它们压缩在一起 with 1 2 3 as nums a b c as letters wat do return zipped a 1 b 2 c 3 可能无法动态分配映射键 例如 使用letters 但此查询将
  • 我可以在运行时以编程方式设置“android:layout_below”吗?

    创建时是否可以相对布局在运行时设置相当于android layout below以编程方式 Yes RelativeLayout LayoutParams params new RelativeLayout LayoutParams Vie
  • 如何在Python中从补丁中恢复3D图像?

    我有一个带有形状的 3D 图像DxHxW 我成功地将图像提取成补丁pdxphxpw 重叠的补丁 对于每个补丁 我都会进行一些处理 现在 我想从处理后的补丁生成图像 以便新图像必须与原始图像具有相同的形状 你能帮我做一下吗 这是我提取补丁的代
  • C# 编译器是否足够智能来优化这段代码?

    请忽略此问题中的代码可读性 从性能上来说 下面的代码是否应该这样写 int maxResults criteria MaxResults if maxResults gt 0 while accounts Count gt maxResul
  • 应用 tm 方法“stemCompletion”时一个变量的多个结果

    我有一个语料库 其中包含 3 个变量 ID 标题 摘要 的 15 个观察结果的期刊数据 我使用 R Studio 从 csv 文件中读取数据 每个观察一行 在执行一些文本挖掘操作时 我在使用 StemCompletion 方法时遇到了一些麻
  • 如何使用共享库运行 LLVM 解释器?

    I have mylib c具有某些功能的文件 我想使用我的这些功能 c文件作为已编译的 llvm 代码中的外部文件 我正在使用 LLVM 解释器 lli 4 0 我想知道我怎么知道lli使用我的函数 c file lli has a lo
  • 如何在delphi10中使用IdHTTP登录Instagram网站

    我正在尝试在 Delphi 中使用 idhttp 登录 Instagram 网站 我使用谷歌浏览器开发者工具来获取数据 这是我到目前为止所尝试过的 procedure TForm1 Button1Click Sender TObject v