File.getResourceKey 在 Google Apps 脚本中一直 returns 为空
File.getResourceKey consistently returns null in Google Apps Script
根据最近来自Google开发人员的一封电子邮件,在 2021 年 9 月 13 日的安全更新后,在获取某些文件和文件夹时需要指定一个 resourceKey。他们说“为了避免错误访问文件,您必须更新访问文件的代码以包含适当的资源密钥。"
获取密钥似乎适用于文件夹:
var folder = DriveApp.getFolderById(folderId);
var folderResourceKey = folder.getResourceKey();
如果我没理解错的话,在安全更新后,开发人员仍然可以使用此方法获取资源密钥(假设文件夹上有适当的共享设置)。然后我们可以在脚本中对资源密钥进行硬编码,以便在 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
在网络浏览器中加载文件后,我尝试单击“文件 -> 文档详细信息”,但它没有显示元数据。
此 API 文档表明在您获取元数据时包含资源密钥,但我认为此页面的上下文不同,我看不出如何从 Google 应用程序脚本:
https://developers.google.com/drive/api/v3/reference/files
即使我能找出可以获取该元数据的上下文,我也不希望它包含 GAS API 未提供的资源密钥。
Google Apps 脚本开发人员有什么方法可以获取文件的资源键吗?
您现在可以在 2021 年 8 月 31 日使用 getSecurityUpdateEnabled
announced:
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, use setSecurityUpdateEnabled
.
来源:https://developers.google.com/apps-script/reference/drive/file#getsecurityupdateenabled
用它来检查文件是否需要资源键:
var file = DriveApp.getFileById(fileId);
if (file.getSecurityUpdateEnabled())
{
// resource key is required for the file
// ..
}
已公布的方法包括获取 files/folders 是否符合安全更新条件的方法:
公布的方法包括设置 files/folders 在使用 link 共享时是否需要资源密钥进行访问的方法:
关于为什么 files/folders 可以 return 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
or type=group
permission. Requests from users that only have access to these link-shared files via a type=domain
or type=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 驱动器文件都应该具有非空资源密钥,除非它是 link 以特定权限共享的,并且不知何故仅旧文件的子集需要。
根据最近来自Google开发人员的一封电子邮件,在 2021 年 9 月 13 日的安全更新后,在获取某些文件和文件夹时需要指定一个 resourceKey。他们说“为了避免错误访问文件,您必须更新访问文件的代码以包含适当的资源密钥。"
获取密钥似乎适用于文件夹:
var folder = DriveApp.getFolderById(folderId);
var folderResourceKey = folder.getResourceKey();
如果我没理解错的话,在安全更新后,开发人员仍然可以使用此方法获取资源密钥(假设文件夹上有适当的共享设置)。然后我们可以在脚本中对资源密钥进行硬编码,以便在 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
在网络浏览器中加载文件后,我尝试单击“文件 -> 文档详细信息”,但它没有显示元数据。
此 API 文档表明在您获取元数据时包含资源密钥,但我认为此页面的上下文不同,我看不出如何从 Google 应用程序脚本:
https://developers.google.com/drive/api/v3/reference/files
即使我能找出可以获取该元数据的上下文,我也不希望它包含 GAS API 未提供的资源密钥。
Google Apps 脚本开发人员有什么方法可以获取文件的资源键吗?
您现在可以在 2021 年 8 月 31 日使用 getSecurityUpdateEnabled
announced:
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
用它来检查文件是否需要资源键:
var file = DriveApp.getFileById(fileId);
if (file.getSecurityUpdateEnabled())
{
// resource key is required for the file
// ..
}
已公布的方法包括获取 files/folders 是否符合安全更新条件的方法:
公布的方法包括设置 files/folders 在使用 link 共享时是否需要资源密钥进行访问的方法:
关于为什么 files/folders 可以 return 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 驱动器文件都应该具有非空资源密钥,除非它是 link 以特定权限共享的,并且不知何故仅旧文件的子集需要。