【问题标题】:"Insufficient Permission for this file" when using Google Advanced Drive API to set contentRestiction.readOnly even if I am the owner使用 Google Advanced Drive API 设置 contentRestiction.readOnly 时出现“此文件权限不足”,即使我是所有者
【发布时间】:2023-03-18 20:25:02
【问题描述】:

我试图防止意外编辑使用一系列 Google 脚本和 Google 表格数据自动生成的 Google 表单。为此,我阅读了一些文档,并从标准 DriveApp 对象切换到 Advanced Drive API(v2 脚本中还没有可用的 v3)来访问 contentRestrictions.readOnly 元数据。

请注意,我使用的是共享云端硬盘,而不是标准的 MyDrive

我写了一个很简单的函数来设置参数为true并设置一个原因:

/**
 * Locks a file given it's unique ID. This should prevent 
 * accidental editing of the file.
 * @param {String} File unique ID
 */
function setFileLock(fileId) {
  
  var resource = {
    contentRestrictions: [{readOnly: true, 
                           reason: 'This file is locked to prevent accidental editing'}]
  }
  
  Drive.Files.patch(resource, fileId, { supportsAllDrives: true });
}

我确实在 appscript.json 中有所有相关的范围:

{
  "timeZone": "Europe/Paris",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Drive",
      "serviceId": "drive",
      "version": "v2"
    }]
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/forms", 
    "https://www.googleapis.com/auth/spreadsheets", 
    "https://www.googleapis.com/auth/script.container.ui", 
    "https://www.googleapis.com/auth/script.external_request", 
    "https://www.googleapis.com/auth/drive", 
    "https://www.googleapis.com/auth/drive.file", 
    "https://www.googleapis.com/auth/drive.metadata",
    "https://www.googleapis.com/auth/drive.readonly",
    "https://www.googleapis.com/auth/drive.metadata.readonly",
    "https://www.googleapis.com/auth/drive.photos.readonly",
    "https://www.googleapis.com/auth/drive.install", 
    "https://www.googleapis.com/auth/drive.appdata", 
    "https://www.googleapis.com/auth/drive.scripts", 
    "https://www.googleapis.com/auth/userinfo.email"],
  "executionApi": {
    "access": "DOMAIN"
  },
  "runtimeVersion": "V8"
}

即使我是文件的所有者,并且在使用 Drive.Files.get 检查权限时,我可以看到我是所有者并且我拥有“组织者”(即内容管理器)权限,我保留收到错误“此文件的权限不足”(403)。请注意,在同一个脚本中,我可以使用 Drive.Files.touch,它实际上可以毫无问题地更新文件。

还有其他人有类似的问题吗?它似乎只与contentRestrictions 属性有关,例如我可以更改titledescription

任何帮助将不胜感激,我有点卡住了。

谢谢!

【问题讨论】:

  • DalmTo,我不确定我是否理解您的问题。我正在使用 Google Apps 脚本,当您运行该脚本时,将启动一个弹出窗口,要求为我在上面发布的所有范围授权该脚本。每次我更改 appscript.json 文件时,都会要求我重新授权。我不知道具体的授权码……也许是我遗漏的那块?
  • 仔细检查您登录的用户是拥有该文件的用户,该错误消息意味着您无权执行您尝试执行的操作。如果它与范围无关,则它必须与用户访问相关。
  • 请提供您的确切请求以检查导致 403 的权限。
  • 正如上面对@DalmTo 的评论,我没有特定的授权请求,我正在使用 Google App Script 高级驱动 API (v2),它是 HTTP API 的包装器。事实上,我确实在 appscript.json 中有范围,这使得带有授权的弹出窗口出现在用户面前。即使我尝试将 Try now (developers.google.com/drive/api/v2/reference/files/…) 与 HTTP 请求一起使用,即使我是文件所有者,它仍然会以 403 失败,并且我可以使用相同的 API 调用更改 titledescription。只有contentRestrictions 返回错误。
  • 你试过Drive.Files.update({'contentRestrictions': [{'readOnly': true}]}) 吗?

标签: google-apps-script google-drive-api


【解决方案1】:

contentRestrictions 不能应用于所有文件类型

它可以工作,例如对于 Google Docs 和 Google Sheets(您可以自己验证),但不是例如对于文件夹,不幸的是,对于 Google 表单都没有。

【讨论】:

  • 该死!这就是问题所在,它适用于电子表格,但不适用于表单。这是暂时的情况还是设计使然?有没有什么地方可以找到这些信息?
  • 我相信这是设计使然。官方信息是有限的——毕竟这个功能是新的。这是我发现的:gsuiteupdates.googleblog.com/2020/10/…docs.google.com/document/d/…
  • 感谢 Ziganotschka,我已阅读相同的文档,其中写道:“您现在可以通过 Drive API 添加和删除内容限制。通过使用新的 ContentRestriction API,Drive 中的任何文件类型都可以“锁定”,防止更改项目的内容、标题和 cmets”...所以我想 任何文件类型也应该包括表单。
  • 在这种情况下,它可能是一个错误,您可以在Google Issue Tracker 上报告。
【解决方案2】:

负载中的readOnly 属性应该是布尔值,而不是字符串。

'readOnly': true

【讨论】:

  • 感谢 Amit Agarwal,我也尝试将整个块作为 javascript 对象传递,但没有任何区别。我认为 API 包装器正在使用 JSON 编码来发送 HTTP 请求。问题是权限。
  • 我写了一个脚本,它使用 Drive API 和 UrlFetchApp 来使文件只读。你能检查一下这是否有效吗? labnol.org/code/read-only-google-drive-file-201011
  • 您已经拥有 API 所需的 https://www.googleapis.com/auth/drive 范围。
  • 感谢@Amit,我已尝试从文档 (developers.google.com/drive/api/v2/reference/files/…) 中执行 API,它使用与您在脚本中使用的相同方法,通过发布直接 HTTP 请求。即使我可以更改文件的标题和描述,它仍然返回 403,没有足够的权限。请注意,我使用的是共享驱动器,而不是我的驱动器,这就是为什么请求需要具有 { supportAllDrives: true }
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2019-08-04
  • 1970-01-01
相关资源
最近更新 更多