4

According to a recent email from GoogleDevelopers, it will be necessary to specify a resourceKey when fetching some files and folders after the security update on the 13th of September 2021. They say "To avoid errors accessing files, you must update your code for accessing files to include the appropriate resource keys."

Fetching the key appears to work for folders:

  var folder = DriveApp.getFolderById(folderId);
  var folderResourceKey = folder.getResourceKey();

If I understand correctly, after the security update it will still be possible for the developer to use this method to fetch the resource key (assuming appropriate sharing settings on the folder). Then we can hardcode the resource key in the script for use in a getFolderByIdAndResourceKey call.

However, when I try this on a File instead of a Folder, I consistently receive "null" for the resource key:

  var file = DriveApp.getFileById(fileId);
  var resourceKey = file.getResourceKey();

I've tried this for several files, including files which I own and which I created months ago, so I don't think it can be an instance of this issue:

Unpredictable result of DriveId.getResourceId() in Google Drive Android API

The API documentation indicates that it should work the same way for Files as for Folders:

https://developers.google.com/apps-script/reference/drive/file?hl=en#getResourceKey()

https://developers.google.com/apps-script/reference/drive/folder?hl=en#getresourcekey

I also tried getTargetResourceKey in case the files were links, but the result was still null:

https://developers.google.com/apps-script/reference/drive/file?hl=en#gettargetresourcekey

After loading the file in the web browser, I tried clicking on "File -> Document details", but it doesn't show the metadata.

This API document indicates that the resource key is included when you fetch the metadata, but I think the context of this page is different and I can't see how to fetch this metadata from Google Apps Script:

https://developers.google.com/drive/api/v3/reference/files

Even if I can work out the context in which I can fetch that metadata, I don't have much hope that it's going to contain the resource key which is not provided by the GAS API.

Is there any way for the Google Apps Script developer to fetch the resource keys for files?

Kos
  • 4,890
  • 9
  • 38
  • 42

1 Answers1

5

You can now use getSecurityUpdateEnabled announced on August 31, 2021:

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.

Source: https://developers.google.com/apps-script/reference/drive/file#getsecurityupdateenabled

Use it to check if file requires resource key:

var file = DriveApp.getFileById(fileId);

if (file.getSecurityUpdateEnabled())
{
  // resource key is required for the file
  // ..
}

Announced methods include methods to get whether files/folders are eligible for security update:

Announced methods include methods to set whether files/folders require a resource key for access when they shared using a link:


General info on why files/folders may return null instead of resource key string:

From current documentation (bold is mine):

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.

So, as far as I can tell, there is no indication that each Google Drive file should have non-null resource key, unless it's link-shared with specific permission, and somehow required only for subset of old files.

Kos
  • 4,890
  • 9
  • 38
  • 42