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.