【发布时间】: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 属性有关,例如我可以更改title 和description。
任何帮助将不胜感激,我有点卡住了。
谢谢!
【问题讨论】:
-
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 调用更改
title和description。只有contentRestrictions返回错误。 -
你试过
Drive.Files.update({'contentRestrictions': [{'readOnly': true}]})吗?
标签: google-apps-script google-drive-api