How to make authentication to log in a website with Google scripts?

我的梦境 提交于 2020-06-27 03:59:29

问题


I want to make an authentication and then post an ad to a website with a Google Apps Script. The plan is like this:

  1. Make an authentication to log in by HTTP post method.
  2. Get response and get cookies needed.
  3. Send a new post request with needed content of an ad and cookies to make the website identify the script as a "logged in user".

I'm stuck on the 1st stage. I made this script:

function sendHttpPost() {
   var options =
   {
     "method" : "post",
     "login[email]" : "mihsav76@gmail.com",
     "login[password]" : "testpas"              
   };

   var response = UrlFetchApp.fetch("http://olx.ua/myaccount/", options);
   var sessionDetails = response.getAllHeaders();       
   Logger.log(response.getContentText());
 }

Credentials I got through Developer Console. Screenshot is attached.

HTML code which I get from response is just a starting log in page. What is done wrong on this stage?


回答1:


You are passing the login parameters as options to UrlFetchApp, but you need to pass them as fields in the POST body. This is done with the 'payload' option.

   var options =
   {
     "method" : "post",
     "payload":{"login[email]" : "mihsav76@gmail.com",
                "login[password]" : "testpas"
               }
   };

See the "advanced parameters" table and examples in the documentation here:

https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params



来源:https://stackoverflow.com/questions/33391239/how-to-make-authentication-to-log-in-a-website-with-google-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!