Skip to content Skip to sidebar Skip to footer

Google Apps Script Urlfetchapp Returns 404 When The Url Exists

I'm trying to retrieve a pdf of a spreadsheet. Whenever I try I get the following error: Request failed for https://docs.google.com/a/firstcallres.com/spreadsheets/d/1ZPcW5cOQT5w2

Solution 1:

You are missing the proper scopes to make this request. It should return a 401 unauthenticated instead of a 404. To fix this you need to add the Drive scope. I do this by a simple DriveApp.getRootFolder() call somewhere in your script. This will add the drive scope to your project. Any call to DriveApp will add the scope though.

Here is an example script:

function exportToPDF() {    
  var url = "https://docs.google.com/spreadsheets/d/1oQ2zsbcCwuc_wdwgfFopRJn-5fGN3oY7L237ivxuxmM/export?exportFormat=pdf"; //add your options as you wishvar res = UrlFetchApp.fetch(url, {headers:{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},muteHttpExceptions:true});
  var newPdf = Utilities.newBlob(res.getContent(), "application/pdf", "Export.pdf")
  DriveApp.createFile(newPdf);
}

Post a Comment for "Google Apps Script Urlfetchapp Returns 404 When The Url Exists"