Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following code in Google Apps Script which retrieves CSV data from a webpage via HTTP using basic authentication and places it into a spreadsheet:

CSVImport.gs

function parseCSVtoSheet(sheetName, url)
{
  // Credentials
  var username = "myusername";
  var password = "mypassword";
  var header = "Basic " + Utilities.base64Encode(username + ":" + password);
  
  // Setting the authorization header for basic HTTP authentication
  var options = {
    "headers": {
      "Authorization": header
    }
  };
  
  // Getting the ID of the sheet with the name passed as parameter
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName(sheetName);
  var sheetId = sheet.getSheetId();
  
  // Getting the CSV data and placing it into the spreadsheet
  var csvContent = UrlFetchApp.fetch(url, options).getContentText();
  var resource = {requests: [{pasteData: {data: csvContent, coordinate: {sheetId: sheetId}, delimiter: ","}}]};
  Sheets.Spreadsheets.batchUpdate(resource, spreadsheet.getId());
}

This has been working up until recently where randomly I get the following error on the UrlFetchApp.fetch line:

Exception: Unexpected error: http://www.myurl.com/data/myfile.csv (line 21, file "CSVImport")

I have tried:

  • Putting the credentials directly in the URL instead of in an Authorization header (I received a different error saying "Login information disallowed").
  • Encoding the credentials to base64 right when I pass it into the headers object (didn't work, same error).
  • Removing authentication altogether (predictably I received a 401 response from the HTTP page).

I'm not sure what else to try and why this randomly broke down all of a sudden. Any advice?

like image 951
Step Avatar asked Dec 10 '20 11:12

Step


1 Answers

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.

like image 132
ziganotschka Avatar answered Oct 17 '22 07:10

ziganotschka