user3427070
user3427070

Reputation: 549

How to retrieve folder by ID in a shared drive with Google Drive API v3

I'm trying to retrieve a specific folder based on a driveId and a folderId. I can easily retrieve a folder by its ID if it's in the user's drive like so:

folder, err := srv.Files.Get(folderId).Do()

If the folderId I pass refers to a folder in a shared drive, then I get a 404 with a message like "googleapi: Error 404: File not found: 1abcdefgh23456789., notFound".

Ideally, I'm looking for something like:

folder, err := srv.Files.Get(folderId).DriveId(driveId).Do()

But that fails because there's no such function ("srv.Files.Get(folderId).DriveId undefined (type *drive.FilesGetCall has no field or method DriveId").

I've tried to workaround this by using the more complex List method:

res, err := srv.Files.List().Q("id = '" + folderId + "' and mimeType = 'application/vnd.google-apps.folder'").SupportsAllDrives(true).IncludeItemsFromAllDrives(true).Corpora("drive").DriveId(driveId).Do()

But that fails at runtime with "googleapi: Error 400: Invalid Value, invalid". I can use name = '<my folder name>' but that's obviously not good because there could be duplicate names.

What is the proper way to get a folder in a shared drive by its ID?

Upvotes: 2

Views: 2258

Answers (2)

ziganotschka
ziganotschka

Reputation: 26836

To obtain a file located on a shared Drive, you need to set the parameter supportsAllDrives to true

See documentation.

Upvotes: 1

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117281

What you are doing is currently correct. The Q parameter for searching is very limited and there is no other way currently to request just the sheared drive.

I suspect what you will need to do is sort though them locally

The file resource has a field called drive id that you could check. Make sure that you add it to fields if its not returning as part of your response.

enter image description here

Upvotes: 1

Related Questions