简体   繁体   中英

Delphi - How to login to the Instagram with TChromium?

...
...
var
  Header:  ICefStringMultimap;
  Data:    ICefPostData;
  request: ICefRequest;
begin
Header := TCefStringMultimapOwn.Create;
Header.Append('Content-Type', 'application/x-www-form-urlencoded');
Data := TCefPostDataRef.New;
Data.AddElement(CreateField('username=nameofuser'));
Data.AddElement(CreateField('&enc_password=#PWD_INSTAGRAM_BROWSER:10:1635078414:AdFQAKoUvWoRP7gLgb1wRaPVapDiZ/Lv08VeSuJ5OmkyqZERI3U/HvRWvWeAKFI+ZBP4l6qQIoApdoUJgb8XJkSD2u8ZECfPaoUczs8+n1zpf8fsV8JIqGJhYCjkRvGBS/3TJBcXdUcUsL3Ncg=='));
request       := TCefRequestRef.New;
request.Flags := UR_FLAG_NONE;
request.SetReferrer('https://www.instagram.com/accounts/login/', uCEFTypes.TCefReferrerPolicy.REFERRER_POLICY_NEVER_CLEAR_REFERRER);
request.Assign('https://www.instagram.com/accounts/login/ajax/', 'POST', Data, Header);
Chromium1.browser.MainFrame.LoadRequest(request);
end;
...
...

When the codes above executed, the respond is:

This page could not be loaded. If you have cookies disabled in your browser, or you are browsing in Private Mode, please try enabling cookies or turning off Private Mode, and then retrying your action.

The Asynchronous javascript (AJAX) call to https://www.instagram.com/accounts/login/ajax is a web service call into the back end made by code called on the login page. It is not a page to navigate the browser to on it's own. It also does a lot of other setup for the call.

If you inspect the normal web page operation using the developer tools available on modern browsers you can see what the web service responds with and it is not a webpage. It returns JSON which is easily consumed by Javascript code. For example on a call that fails to login due to both a user and password that are not valid it returns:

{
    "user": false,
    "authenticated": false,
    "status": "ok"
}

One method to automate web applications like this is to set element values and trigger events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM