使用UrlFetchApp的未经授权的错误401

时间:2018-02-13 11:55:55

标签: google-apps-script google-api google-oauth google-spreadsheet-api

我正在尝试创建一个webapp,其中一部分将允许用户在我的驱动器中创建一个新的电子表格文件。 fileURL是指向excel文件的链接(从文件柜中获取的下载链接)。

我正在努力传授权限,以便可以创建Google电子表格。我收到了未经授权的错误401.我已经读过DriveApp.getRootFolder()添加了驱动器范围,但我在某处出错。

function downloadFile(fileURL, folder) {
  var filename = fileURL.match(".+/(.+?)([\?#;].*)?$")[1];
  var auth = ScriptApp.getOAuthToken();
  var header = {"authorization": "Bearer " + auth};
  var params = { 'method':'post', 'headers':header, 'muteHttpExceptions':true};

  var holder = DriveApp.getRootFolder();  
  var response = UrlFetchApp.fetch(fileURL, params);
  var rc = response.getResponseCode();
  var blob = response.getBlob();

//delete file if it already exists
  var fileList = DriveApp.getFilesByName(filename);
  while (fileList.hasNext()) {
    var fileId = fileList.next().getId();
    DriveApp.getFileById(fileId).setTrashed(true);
  }

  var resource = {
    "mimeType": "application/vnd.google-apps.spreadsheet",
    "parents": [{id: folder}],
    "title": filename
  }
//Create file
  var res = Drive.Files.insert(resource, blob); //put
}

0 个答案:

没有答案