根据 GoogleDevelopers 最近的一封电子邮件,在 2021 年 9 月 13 日的安全更新之后,在获取某些文件和文件夹时需要指定一个 resourceKey。他们说“为了避免访问文件时出错,您必须更新访问文件的代码包括适当的资源 key 。”
获取 key 似乎适用于文件夹:
var folder = DriveApp.getFolderById(folderId);
var folderResourceKey = folder.getResourceKey();
如果我理解正确,在安全更新后,开发人员仍然可以使用此方法获取资源 key (假设文件夹上的共享设置适当)。然后我们可以在脚本中对资源键进行硬编码,以便在 getFolderByIdAndResourceKey 调用中使用。但是,当我在文件而不是文件夹上尝试此操作时,我始终收到资源键的“null”:
var file = DriveApp.getFileById(fileId);
var resourceKey = file.getResourceKey();
我已经为几个文件尝试了这个,包括我拥有的文件和我几个月前创建的文件,所以我认为它不是这个问题的一个实例:Unpredictable result of DriveId.getResourceId() in Google Drive Android API
API 文档表明它应该以与文件夹相同的方式工作:
https://developers.google.com/apps-script/reference/drive/file?hl=en#getResourceKey()
https://developers.google.com/apps-script/reference/drive/folder?hl=en#getresourcekey
我还尝试了 getTargetResourceKey 以防文件是链接,但结果仍然为空:
https://developers.google.com/apps-script/reference/drive/file?hl=en#gettargetresourcekey
在 Web 浏览器中加载文件后,我尝试单击“文件 -> 文档详细信息”,但它没有显示元数据。
此 API 文档表明在获取元数据时包含资源 key ,但我认为此页面的上下文不同,我无法看到如何从 Google Apps 脚本获取此元数据:
https://developers.google.com/drive/api/v3/reference/files
即使我可以计算出可以获取该元数据的上下文,我也不太希望它包含 GAS API 未提供的资源 key 。
Google Apps Script 开发人员有没有办法获取文件的资源键?
最佳答案
您现在可以使用 getSecurityUpdateEnabled
announced 2021 年 8 月 31 日:
Gets whether this
File
requires a resource key for access when it's shared using a link. This requirement is turned on by default for eligible files and folders. To turn the resource key requirement on or off for eligible files, usesetSecurityUpdateEnabled
.
来源:https://developers.google.com/apps-script/reference/drive/file#getsecurityupdateenabled
用它来检查文件 是否需要 资源 key :
var file = DriveApp.getFileById(fileId);
if (file.getSecurityUpdateEnabled())
{
// resource key is required for the file
// ..
}
公布的方法包括获取文件/文件夹是否有资格进行安全更新的方法:File.getSecurityUpdateEligible()
/ Folder.getSecurityUpdateEligible()
公布的方法包括设置文件/文件夹在使用链接共享时是否需要资源 key 才能访问的方法:
File.setSecurityUpdateEnabled(enabled)
/ Folder.setSecurityUpdateEnabled(enabled)
关于为什么文件/文件夹可能返回的一般信息
null
而不是资源键字符串:从当前文档(粗体是我的):
Access link-shared files using resource keys
A file that is shared with the link can only be discovered by users that can access the file as a result of a
type=user
ortype=group
permission. Requests from users that only have access to these link-shared files via atype=domain
ortype=anyone
permission, may require a resource key.This resource key requirement only applies to a subset of old files.
For more information about permissions, see Share files, folders and drives.
因此,据我所知,没有迹象表明每个 Google Drive 文件都应该具有非空的资源键,除非它以特定权限进行链接共享,并且以某种方式只需要旧文件的子集。
关于google-apps-script - File.getResourceKey 在 Google Apps 脚本中始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68751714/