如何使用Delphi 10中的http组件获取Instagram以下列表

时间:2016-06-27 15:19:40

标签: delphi instagram delphi-10-seattle idhttp

我正在尝试仅使用http组件获取我的Instagram“关注”列表。我试过使用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;

1 个答案:

答案 0 :(得分:4)

在这一行:

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

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

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

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

但是,如果您查看Web浏览器发出的实际HTTP请求,您会看到单击配置文件页面上的Following链接实际上会向以下URL发送AJAX POST请求接收列出关注者的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应该收到包含列表中前10位粉丝的JSON。