如何使用Google Drive API v3通过文件夹ID检索共享驱动器中的文件夹

huangapple go评论87阅读模式
英文:

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

问题

我正在尝试根据driveId和folderId检索特定的文件夹。如果文件夹在用户的驱动器中,我可以通过其ID轻松检索文件夹,如下所示:

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

如果我传递的folderId是指共享驱动器中的文件夹,则会收到一个404错误,错误消息类似于"googleapi: Error 404: File not found: 1abcdefgh23456789., notFound"。

理想情况下,我希望有类似以下的方法:

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

但是这会失败,因为没有这样的函数("srv.Files.Get(folderId).DriveId undefined (type *drive.FilesGetCall has no field or method DriveId)")。

我尝试通过使用更复杂的List方法来解决这个问题:

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

但是在运行时会出现"googleapi: Error 400: Invalid Value, invalid"错误。我可以使用name = '<我的文件夹名称>',但这显然不好,因为可能存在重复的名称。

获取共享驱动器中文件夹的正确方法是什么?

英文:

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(&quot;id = &#39;&quot; + folderId + &quot;&#39; and mimeType = &#39;application/vnd.google-apps.folder&#39;&quot;).SupportsAllDrives(true).IncludeItemsFromAllDrives(true).Corpora(&quot;drive&quot;).DriveId(driveId).Do()

But that fails at runtime with "googleapi: Error 400: Invalid Value, invalid". I can use name = &#39;&lt;my folder name&gt;&#39; 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?

答案1

得分: 1

你现在所做的是正确的。搜索时的 Q 参数非常有限,目前没有其他方法可以仅请求剪切驱动。

我怀疑你需要在本地进行排序。

file resource 有一个名为 drive id 的字段,你可以检查它。确保将其添加到 fields 中,如果它没有作为响应的一部分返回。

如何使用Google Drive API v3通过文件夹ID检索共享驱动器中的文件夹

英文:

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.

如何使用Google Drive API v3通过文件夹ID检索共享驱动器中的文件夹

答案2

得分: 0

要获取位于共享驱动器上的文件,您需要将参数supportsAllDrives设置为true


请参阅文档

英文:

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

See documentation.

huangapple
  • 本文由 发表于 2021年8月16日 07:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/68796023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定