title: URL fetch sometimes leads to "Uncaught ScriptError" resp. 404
body: <p>My goal is to write an Add-on, which converts a Spreadsheet file into an XLSX-file and sends it to a given mail distribution list on a daily, weekly or monthly basis.</p>
  
  <p>Everything works fine, except from the URL fetch. Please find my function below:</p>
  
  <pre><code>function startWizard () {
  var addresses = SpreadsheetApp.getActive().getSheetByName("Einstellungen").getRange("A1").getValues();
  var filename = SpreadsheetApp.getActiveSpreadsheet().getName();
  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
  var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + ssID + "&amp;exportFormat=xlsx"; 
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url, {headers: {'Authorization': 'Bearer '+ token}});
  Logger.log(url);
  var contents = response.getContent();
  
  MailApp.sendEmail(addresses,"subject" ,"body", {attachments:[{fileName:filename+".xlsx", content:contents, mimeType:"application//xlsx"}]});
  }
  </code></pre>
  
  <p>Most of the time everything works fine and I receive an e-mail and the XLSX-file attached. Nevertheless, from time to time I get the following error message in my browser console (translated from German into English) and I do not receive any e-amil:</p>
  
  <p>"Uncaught ScriptError: Error at the request for <a href="https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1iBIny93-y1sSsGxwn78_c-_9EpjIPwMBGa1Ivn1DNKg&amp;exportFormat=xlsx" rel="nofollow">https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1iBIny93-y1sSsGxwn78_c-_9EpjIPwMBGa1Ivn1DNKg&amp;exportFormat=xlsx</a>. The following code was responded: 404. Shortened server answer: "GOOGLE ANSWER IN HTML CODE" Use "muteHttpExceptions", to read the whole answer.</p>
  
  <p>I have tried out other solutions but nothing worked so far. In addition I tried to search for an answer in this forum, but in vain.</p>
  
  <p>It seems that the URL is correct, as I can download the file. My assumption is that there is something wrong with the oAuth.</p>
  
  <p>Could somebody please give me a hint? It is driving me mad...</p>
  
  <p>Thank you!</p>