0

I want to know if I'm missing something. I am using google's sample code from their Documentation:

  var username = "myUserName";
  var password = "myPsw";
  var url = "http://myurl.com/ReportServer?/ExternalData/SCCOfficeHuddleCount&rs:Format=CSV";
  
  var authHeader = 'Basic ' + Utilities.base64Encode(username + ':' + password);
  var options = {
    headers: {Authorization: authHeader}
  }

  var response = UrlFetchApp.fetch(url, options);

but still getting 401 - unauthorized. I don't know if I'm missing something.

I already validate that the URL is working. If I paste this URL below in the browser directly

http://user:[email protected]/ReportServer?/ExternalData/SCCOfficeHuddleCount&rs:Format=CSV

I get the CSV file. So how can I do this using AppScript?

7
  • Does it work if you just fetch the same url you're using manually? UrlFetchApp.fetch('http://user:[email protected]/ReportServer?/ExternalData/SCCOfficeHuddleCount&rs:Format=CSV') Commented Jan 17, 2022 at 20:40
  • @AaronDuniganAtLee No, It returns "Exception: Login information disallowed". It works if I paste the URL in another tab of the browser directly. Commented Jan 17, 2022 at 20:46
  • Your code looks correct, so I suspect it's an issue that the url in question is expecting a different form of authorization. Can you confirm that basic auth with base64 of "username:password" is what it expects? Sometimes you can tell by using the browser dev tools to inspect the headers that are sent with your successful browser request. Commented Jan 17, 2022 at 20:56
  • 1
    You might also try encodeURI(url) before fetching (in both your existing code, and in my previous suggestion), in case there are special characters in the url that need to be escaped. Commented Jan 17, 2022 at 21:00
  • 1
    @TheMaster Yes, I encoded the URL, thanks for the help. The main issue was a config with the server auth. Commented Jan 18, 2022 at 18:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.