OAuth error when exporting Sheet as XLS in Google Apps Script

后端 未结 1 758
清歌不尽
清歌不尽 2020-12-20 09:07

I had a Google Apps Script to take appointments from my Google Calendar, copy them into a Google Sheet, convert it to XLS and email it. It was working fine until this week.<

相关标签:
1条回答
  • 2020-12-20 09:25

    Instead of using OAuthConfig (which must be auth'ed in the Script Editor) you can pass an OAuth2 token instead, retrievable via ScriptApp.getOAuthToken().

    The code snippet below uses the Advanced Drive service to get the export URL, but if you hand construct the URL you'll need to ensure that the Drive scope is still requested by your script (simply include a call to DriveApp.getRootFolder() somewhere in your script code).

    function exportAsExcel(spreadsheetId) {
      var file = Drive.Files.get(spreadsheetId);
      var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];
      var token = ScriptApp.getOAuthToken();
      var response = UrlFetchApp.fetch(url, {
        headers: {
          'Authorization': 'Bearer ' +  token
        }
      });
      return response.getBlob();
    }
    
    0 讨论(0)
提交回复
热议问题