Unexpected error on UrlFetchApp.fetch in Google Apps Script using basic authentication

This is related to a new bug, see here

Many users are affected, I recommend you to "star" the issue to increase visibility and hopefully accelerate the process.


I had the same situation. At that time, I could noticed that when the built-in function of Google Spreadsheet is used for the URL, the values can be retrieved. In that case, as the current workaround, I used the following flow.

  1. Put a formula of =IMPORTDATA(URL).
  2. Retrieve the values from the sheet.

When above flow is reflected to your URL of http://www.myurl.com/data/myfile.csv, it becomes as follows.

About basic authorization for URL:

When I saw your script, I confirmed that you are using the basic authorization. In this case, the user name and password can be used for the URL like http://username:[email protected]/data/myfile.csv.

From your script, when the values of username and password are myusername and mypassword, respectively, you can use the URL as http://myusername:[email protected]/data/myfile.csv.

Here, there is an important point. If the specific characters are included in username and password, please do the url encode for them.

Sample script:

function myFunction() {
  const url = "http://myusername:[email protected]/data/myfile.csv";  // This is your URL.

  // Retrieve the values from URL.
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName(sheetName);
  sheet.clear();
  var range = sheet.getRange("A1");
  range.setFormula(`=IMPORTDATA("${url}")`);
  
  // Retrieve the values from sheet to an array.
  SpreadsheetApp.flush();
  var values = sheet.getDataRange().getValues();
  range.clear();
  console.log(values)
}
  • When above script is run, the values from the URL are put to the sheet, and the values are retrieved as 2 dimensional array for values. If you want to leave only values without the formula, I think that you can copy and paste the values.
  • In this answer, I used IMPORTDATA. But for each situation, other functions might be suitable. In that case, please check them.

Note:

  • This is the current workaround. So when this issue was removed, I think that you can use your original script.

References:

  • IMPORTDATA
  • setFormula()