问题
I want to make an authentication and then post an ad to a website with a Google Apps Script. The plan is like this:
- Make an authentication to log in by HTTP post method.
- Get response and get cookies needed.
- 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