I'm attempting to logon to a website with Google Apps Script and I wrote a function to do this. However I received an error message that said cookies need to be enable. I actually don't understand what the issue is.
I'm attempting to logon to a website with Google Apps Script. I read the following solution, and then wrote the logon function below.
How to use UrlFetchApp with credentials? Google Scripts
I do have cookies enabled in my browser, so could someone please clarify what this message means and how can I fix it.
function logon() {
var myurl = "https://www.someurl.com";
var user = "name";
var password = "########";
var options = {};
options.headers = {"Authorization": "Basic " + Utilities.base64Encode(user + ":" + password) };
var response = UrlFetchApp.fetch(myurl, options);
var ui = HtmlService.createHtmlOutput(response.getContentText());
SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
}
The result was a dialog message that said:
In order to use this page, your browser needs to accept cookies from www.someurl.com
Please enable cookies in your browser preferences and reload this page.
Based on UrlFetchApp not connecting to public URL, I revised my code to the following. However, it still results in the same error message.
function fetchUrlWithCookie() {
var url = "https://www.someurl/login";
var user = "name";
var password = "#####";
var options = {muteHttpExceptions: true,
followRedirects: false};
options.headers = {"Authorization": "Basic " + Utilities.base64Encode(user + ":" + password) };
var response = UrlFetchApp.fetch(url, options);
var cookie = response.getAllHeaders()['Set-Cookie'];
var opt2 = {muteHttpExceptions: true,
"headers":
{Cookie: cookie}
};
var response2 = UrlFetchApp.fetch("https://www.someurl/app/v4/dashboard",opt2);
var ui = HtmlService.createHtmlOutput(response2.getContentText());
SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
Logger.log(response2.getContentText());
}
/* In order to use this page, your browser needs to accept cookies from www.someurl.com
Please enable cookies in your browser preferences and reload this page.
*/