logging on with GAS

71 views
Skip to first unread message

Leland Hovey

unread,
Oct 20, 2019, 1:30:40 PM10/20/19
to Google Apps Script Community
I tried the code below to login to a website, except it produce the error I've listed.

<pre><code>
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());
}
</code></pre>

-----------------------------------------------------------------------------------------------------------------------------------------------------

  /*
      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.
  */

Riël Notermans

unread,
Oct 21, 2019, 1:25:56 AM10/21/19
to Google Apps Script Community
This really depends on the service you are calling. What I know is that many Times the site does a redirect. GAS just does it but you need to add the cookies from that as well, they are skipped now.

Use followRedirects: false I your options

Check if you have a 30x response code and if yes do the redirect manual, the URL is in the location: header.





Op zo 20 okt. 2019 19:30 schreef Leland Hovey <leland...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/9dde37cc-56bd-4023-ada2-6f081f9b8159%40googlegroups.com.

Leland Hovey

unread,
Nov 14, 2019, 2:03:10 PM11/14/19
to Google Apps Script Community

Could you please explain the 3rd item a little more. What steps are required to obtain the response code?


On Monday, October 21, 2019 at 1:25:56 AM UTC-4, Riël Notermans wrote:
This really depends on the service you are calling. What I know is that many Times the site does a redirect. GAS just does it but you need to add the cookies from that as well, they are skipped now.

Use followRedirects: false I your options

Check if you have a 30x response code and if yes do the redirect manual, the URL is in the location: header.





Op zo 20 okt. 2019 19:30 schreef Leland Hovey <lelan...@gmail.com>:
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Michael Ellis

unread,
Nov 17, 2019, 11:37:59 PM11/17/19
to Google Apps Script Community
Lucas,
I tried to use your code with www.mastercook.com, but not with complete success.  I don't get any error message, but the demo window that is displayed over my spreadsheet shows the login screen from mastercook.   In my program, I have given the user name and password.   I don't seem to know how to get my code to pass the user name and password into the correct fields on the login page.   Is that something you have figured out?  Maybe we could team up and see if together we could make progress on this?
Thanks.
Mike
Reply all
Reply to author
Forward
0 new messages