javascript - 即使我是所有者并获得许可,Google Drive + Script 也会抛出权限错误

标签 javascript google-apps-script google-calendar-api

我正在尝试在 12 小时计时器触发器上创建一个基本脚本,该脚本通过其 ICAL URL 循环遍历我的每个 Google 日历,并为我的 Google Drive 上的文件夹下载 ICAL(用于备份目的)。它抛出这个错误

"No item with the given ID could be found, or you do not have permission to access it. (line 23, file "Code")"   (Line #23 is var folder... )

运行脚本确实会在第一次运行循环时下载并保存 ICAL 文件(如果我手动传入每个唯一的 ICAL URL,一次一个),但错误会终止循环。鉴于我已经获得访问权限并且是这里所有内容的所有者,我不确定我还需要什么。

var calendarsToSave = [
                     "https://calendar.google.com/calendar/ical/inXXXXXXX.com/privateXXXX/basic.ics",
                     "https://calendar.google.com/calendar/ical/XXXXX.com_XXXXXXup.calendar.google.com/private-XXXXXXX/basic.ics"
                    ]; 

var folder = '123xxxxxxxxv789';  // my gdrive folder 


function downloadFile(calendarURL,folder) {

   var fileName = "";
   var fileSize = 0;

  for (var i = 0; i < calendarsToSave.length; i++) { 

    var calendarURL = calendarsToSave[i];

    var response = UrlFetchApp.fetch(calendarURL, {muteHttpExceptions: true});
    var rc = response.getResponseCode();

    if (rc == 200) {
      var fileBlob = response.getBlob()

      var folder = DriveApp.getFolderById(folder);   // << returns a permissions error thus terminating the for loop
      var file = folder.createFile(fileBlob);
      fileName = file.getName();
      fileSize = file.getSize();

    }

    var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
    return fileInfo;    

  }  // end for loop

}

最佳答案

更新:您还从参数中重新初始化了一个已经存在的变量,并将其作为全局变量,这样我们就可以删除参数,如果你想保留全局变量。

我们还可以移动您获取 Google 文件夹对象的位置。它每次都保持不变,因此我们不需要再次检索它。

var calendarsToSave = [
                     "https://calendar.google.com/calendar/ical/inXXXXXXX.com/privateXXXX/basic.ics",
                     "https://calendar.google.com/calendar/ical/XXXXX.com_XXXXXXup.calendar.google.com/private-XXXXXXX/basic.ics"
                    ]; 

var folder = '123xxxxxxxxv789';  // my gdrive folder 


function downloadFile(calendarURL) {

   var fileName = "";
   var fileSize = 0;
   var gfolder = DriveApp.getFolderById(folder);

  for (var i = 0; i < calendarsToSave.length; i++) { 

    var calendarURL = calendarsToSave[i];

    var response = UrlFetchApp.fetch(calendarURL, {muteHttpExceptions: true});
    var rc = response.getResponseCode();

    if (rc == 200) {
      var fileBlob = response.getBlob()

      var file = gfolder.createFile(fileBlob);
      fileName = file.getName();
      fileSize = file.getSize();

    }

    var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
    return fileInfo;    

  }  // end for loop

}

让我们看看它能把我们带到哪里。

关于javascript - 即使我是所有者并获得许可,Google Drive + Script 也会抛出权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936453/

相关文章:

javascript - WebSocket 连接失败。 Websocket 握手期间出错。响应代码 403?

javascript - 关闭弹出的小框

javascript - Google APP SCRIPT -> 在给定的持续时间后强制提交 google 表单

css - 如何为 gmail 附加组件编写自定义 CSS?

php - Google 日历 session 链接未通过 Google Calendar API (PHP) 自动创建

google-api - 如何使用谷歌日历API进行人事项目(无需验证)

javascript - 我怎样才能暂停 JavaScript 直到输入,比如确认/警告框?

javascript - Mongoose:如何在变量给定的索引处更新数组中的值?

google-apps-script - 每次进行编辑时将 Google 电子表格导出为 .XLSX

plone - 在 Plone 站点中显示 Google 日历