2

I have code that I copied from here:

How to login to Instagram website using IdHTTP in delphi10

procedure Tinstfrm.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.Lines.add(Reply);

      Reply := lHTTP.Get('https://www.instagram.com/web/friendships/Value/follow/');

      Memo1.Lines.add(Reply);



    finally
      lHTTP.Free;
    end;
  finally
    Params.Free;
  end;
end;

I use it to login into my Instagram account and it is successful, but now I want to Follow some user on Instagram. How can I do that?

Lets say I have this URL call:

lHTTP.Get('https://www.instagram.com/web/friendships/21193118/follow/'); 

I try to use TIdHTTP.Post() like this:

Reply := lHTTP.Post('https://www.instagram.com/web/friendships/21193118/follow/', ...);

Here I should use a parameter but what should I use?

What should I add to the Post params to get the follow to work?

Added some code

I searched in other questions and came up with this code:

procedure Tinstfrm.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.Lines.add(Reply);

      // 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/web/friendships/21193118/follow/', TStream(nil));

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


      // then i dont know what to do

      Memo1.Lines.add(Reply);



    finally
      lHTTP.Free;
    end;
  finally
    Params.Free;
  end;
end;

Here is the HTTP request that I am targeting:

Request URL:https://www.instagram.com/web/friendships/21193118/follow/
Request Method:POST
Status Code:200 
Remote Address:31.13.93.52:443

Header response

cache-control:private, no-cache, no-store, must-revalidate
content-language:en
content-length:39
content-type:application/json
date:Mon, 11 Jul 2016 10:37:37 GMT
expires:Sat, 01 Jan 2000 00:00:00 GMT
pragma:no-cache
set-cookie:csrftoken=ilINmCvcI2X1X5l2ckJ2wgv6nk9Dlmhx; expires=Mon, 10-Jul-2017 10:37:37 GMT; Max-Age=31449600; Path=/
set-cookie:s_network=; expires=Mon, 11-Jul-2016 11:37:37 GMT; Max-Age=3600; Path=/
set-cookie:ds_user_id=3523180621; expires=Sun, 09-Oct-2016 10:37:37 GMT; Max-Age=7776000; Path=/
status:200
strict-transport-security:max-age=86400
vary:Cookie, Accept-Language

Header request

:authority:www.instagram.com
:method:POST
:path:/web/friendships/21193118/follow/
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, br
accept-language:en-US,en;q=0.8
content-length:0
content-type:application/x-www-form-urlencoded
cookie:mid=V3lV5AAEAAF6YI60VulF4Nh4TdmX; sessionid=IGSC0e057587ba07cd4ead550197071f14d12080d91ab64704da189632ff0a3a86e5%3ApNr5RyGSRcCfUXfhbWRVMf5QJmNhMuHO%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A3523180621%2C%22_token%22%3A%223523180621%3AbQbg9P4AtN5pDpi9ecPcm4wCA7pLSZoz%3A9629557a5546b220841ceaf5ac65b6e867bff0e489651b5892608da6520a3453%22%2C%22asns%22%3A%7B%2245.243.46.26%22%3A24863%2C%22time%22%3A1468232916%7D%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1468232915.569637%2C%22_platform%22%3A4%2C%22_auth_user_hash%22%3A%22%22%7D; ig_pr=1; ig_vw=1440; s_network=; csrftoken=ilINmCvcI2X1X5l2ckJ2wgv6nk9Dlmhx; ds_user_id=3523180621
origin:https://www.instagram.com
referer:https://www.instagram.com/50cent/
user-agent:Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
x-csrftoken:ilINmCvcI2X1X5l2ckJ2wgv6nk9Dlmhx
x-instagram-ajax:1
x-requested-with:XMLHttpRequest

I don't want to use the Instagram API, I would like to stimulate this with TIdHTTP.

I tried the following:

Reply := lHTTP.post('https://www.instagram.com/web/friendships/21193118/follow/', token);

But I got this error:

Cannot open file "C:\Users\usr\Desktop\app\Win32\Release\L7jHk1Oxrp5kFZcPxew63xzvhyEasA9Y". The system cannot find the file specified.

Community
  • 1
  • 1
Vlark.Lopin
  • 762
  • 10
  • 21
  • 4
    We've been over this before. Just like the other Intragram URLs you have asked how to interact with, the procedure is the same - use a sniffer to look at the AJAX requests that a web browser makes and then simulate those same requests with `TIdHTTP`. And then while you are at it, PLEASE STOP SIMULATING INSTAGRAM'S AJAX REQUESTS and use [Instagram's developer API](https://www.instagram.com/developer/) instead. Like it or not, it is what you *should* be using. Use the system the way it is designed to be used. You are not making a web browser, so stop trying to act like one. Use the API instead. – Remy Lebeau Jul 10 '16 at 22:30
  • AJAX request added to the question , i dont want to use instagram api i just wanted to done it with simulation – Vlark.Lopin Jul 11 '16 at 10:44
  • Why don't you want to use API? Because you are lazy? You added header and response but where are those POST data? – smooty86 Jul 11 '16 at 12:29
  • @smooty86 read in the question after edit you don't see " i tried the following " ? `Reply := lHTTP.post('https://www.instagram.com/web/friendships/21193118/follow/', token);` and about the API no i dont want use it not because i am sleepy or lazy i just want to use `idhttp` as simulator to post follow or like from an exe application – Vlark.Lopin Jul 11 '16 at 12:40
  • 2
    I saw that. Why do you send a token as POST data? That is wrong. You have request and response header, that is useless now. You have to sniff the POST data. If you used API, it would be simple as `https://api.instagram.com/v1/users/{user-id}/relationship?access_token=ACCESS-TOKEN` – smooty86 Jul 11 '16 at 13:49
  • Per Instagram's [Terms of Use](https://www.facebook.com/help/instagram/478745558852511): "*By accessing or using the Instagram website, ... you agree to be bound by these terms of use ("Terms of Use")... We prohibit crawling, scraping, caching or **otherwise accessing any content on the Service via automated means**, including but not limited to, user profiles and photos (except as may be the result of standard search engine protocols or technologies used by a search engine with Instagram's express consent).*". – Remy Lebeau Jul 11 '16 at 14:57
  • Per Instagram's [Platform Policy](https://www.instagram.com/about/legal/terms/api/): "*We provide the Instagram APIs to support several types of apps and services... Don't use the Instagram API to simply display User Content, import or backup content, or **manage Instagram relationships, without our prior permission**... Don't use the Instagram APIs to **replicate** or attempt to replace **the functionality or essential user experiences of Instagram.com** or any of Instagram's apps... Don't reverse engineer the Instagram APIs or any of Instagram's apps.*" – Remy Lebeau Jul 11 '16 at 14:58
  • As for the *Cannot open file* error, that is because you are passing a token string to a version of `Post()` that expects a filename as input. That version opens the specified file and posts its content to the server. That is not what you need. You need to use a `TStringList` to post webform parameters, and a `TStream` like `TMemoryStream` or `TStringStream` to post JSON queries. – Remy Lebeau Jul 11 '16 at 15:05
  • @RemyLebeau i see your answer in another question and i read the chat that op asked for how to do a like and its seems you solved it but no code were added to understand what should i do to post like or follow what should i add to the Tstringlist already Param can be cleared and add some data but what should i add ? – Vlark.Lopin Jul 11 '16 at 16:33
  • @RemyLebeau oh ok i understand now i get it to work thanks – Vlark.Lopin Jul 11 '16 at 17:15

0 Answers0