如何进行身份验证以使用Google脚本登录网站?

问题描述:

我想进行身份验证,然后使用Google Apps脚本将广告发布到网站上. 计划是这样的:

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. 进行身份验证以通过HTTP post方法登录.
  2. 获取响应并获取所需的cookie.
  3. 发送新的帖子请求,其中包含所需的广告内容和cookie,以使网站将脚本识别为登录用户".

我被困在第一阶段. 我编写了这个脚本:

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代码只是一个初始登录页面.在这个阶段做错了什么?

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

您要将登录参数作为选项传递给UrlFetchApp,但是您需要将它们作为POST正文中的字段传递.这是通过有效载荷"选项完成的.

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://开发人员. google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params