如何在 Delphi 10 中使用 http 组件获取 Instagram 关注列表

How to get Instagram following list using http component in Delphi 10

我正在尝试仅使用 http 组件获取我的 instagram "following" 列表。我试过使用 lHTTP.Get('https://www.instagram.com/Myusername/following/'); 但解密的 html 中没有用户名。但是,我看到一些人在没有 instagram api 的情况下使用它,只是 VB.Net 中的 http 响应。我正在使用 Delphi 10.

更新

procedure TForm1.Button4Click(Sender: TObject);
 var
  lHTTP: TIdHTTP;
  IdSSL: TIdSSLIOHandlerSocketOpenSSL;
  Params, login : TStrings;
  Reply, Token, X: string;
  Cookie: TIdCookie;
 begin

try
Params := TStringList.Create;
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;
  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;

  try
  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);

  finally
  end;

  finally
  end;

Finally

  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/myusername/following/', TStream(nil));
  Memo1.Lines.Add(Reply);


     Finally
    end;
end;



 end;

这一行:

lHTTP.Get('https://www.instagram.com/myusername/following/', TStream(nil));

您告诉 Get() 忽略响应正文 (AResponseContent=nil),然后您没有将新响应分配给 Reply 变量,因此您显示的是旧响应Reply 来自早期登录响应的值。

要获取 /following 页面的 HTML,请改用此

Reply := lHTTP.Get('https://www.instagram.com/myusername/following/');

但是,如果您查看 Web 浏览器发出的实际 HTTP 请求,您会发现点击个人资料页面上的 Following link 实际上会发送一个 AJAX POST 请求以下 URL 接收 JSON 列出关注者的文档:

https://www.instagram.com/query/

POST 正文中包含查询字符串。您需要复制那个 AJAX 请求,例如:

var
  //...
  userid: string; // <-- add this
begin
  // after your AJAX login...

  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.Request.Connection := 'keep-alive';
  lHTTP.Get('https://www.instagram.com/myusername/', TStream(nil));

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

  cookie := lHTTP.CookieManager.CookieCollection.Cookie['ds_user_id', 'www.instagram.com'];
  if cookie <> nil then
    userid := cookie.Value; // <-- add this

  Params.Clear;
  Params.Add('q=ig_user(' + userid + ') {'+LF+
           '  follows.first(10) {'+LF+
           '    count,'+LF+
           '    page_info {'+LF+
           '      end_cursor,'+LF+
           '      has_next_page'+LF+
           '    },'+LF+
           '    nodes {'+LF+
           '      id,'+LF+
           '      is_verified,'+LF+
           '      followed_by_viewer,'+LF+
           '      requested_by_viewer,'+LF+
           '      full_name,'+LF+
           '      profile_pic_url,'+LF+
           '      username'+LF+
           '    }'+LF+
           '  }'+LF+
           '}'+LF);
Params.Add('ref=relationships::follow_list');

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/myusername/';
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/query/', Params);

// process Reply as needed ...

现在 Reply 应该会收到 JSON,其中包含您列表中的前 10 个关注者。