谷歌云盘 API 错误 – “message”: “未找到共享驱动器: xyz”

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

Google Drive API error - "message": "Shared drive not found: xyz"

问题

我正在使用一个服务账号连接到我的个人Google账号中的共享驱动。Google Drive API一直返回一个错误,说找不到共享驱动。我尝试了以下两种方法:

  • 将共享驱动设置为任何人都可以访问的公开状态。
  • 使用服务账号的电子邮件地址为特定用户(即服务账号)添加权限。

共享驱动的链接格式如下:https://drive.google.com/drive/folders/xyz
我假设driveId是链接的最后一部分xyz,那么这是文件夹ID吗?如果是的话,我该如何找到driveId?

  1. // 加载服务账号凭据
  2. data, err := ioutil.ReadFile("service-account.json")
  3. if err != nil {
  4. log.Fatal("无法读取JSON文件")
  5. }
  6. // 解析凭据文件
  7. conf, err := google.JWTConfigFromJSON(data, drive.DriveReadonlyScope)
  8. if err != nil {
  9. log.Fatal("无法解析JSON文件")
  10. }
  11. apiKeyBytes, err := ioutil.ReadFile("api-key.txt")
  12. API_KEY := string(apiKeyBytes)
  13. DRIVE_ID := "1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
  14. // 使用所有参数发送GET请求
  15. client := conf.Client(context.Background())
  16. parameters := "?key=" + API_KEY
  17. parameters += "&corpora=drive"
  18. parameters += "&includeItemsFromAllDrives=true"
  19. parameters += "&supportsAllDrives=true"
  20. parameters += "&driveId=" + DRIVE_ID
  21. response, err := client.Get("https://www.googleapis.com/drive/v3/files" + parameters)
  22. // 读取并打印响应
  23. data_buffer := make([]byte, 2048)
  24. _, err = response.Body.Read(data_buffer)
  25. response.Body.Close()
  26. fmt.Println(string(data_buffer))

当运行此程序时,输出如下:

  1. {
  2. "error": {
  3. "errors": [
  4. {
  5. "domain": "global",
  6. "reason": "notFound",
  7. "message": "找不到共享驱动:1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm",
  8. "locationType": "parameter",
  9. "location": "driveId"
  10. }
  11. ],
  12. "code": 404,
  13. "message": "找不到共享驱动:1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
  14. }
  15. }

我还尝试了此链接上的“尝试此API”工具:https://developers.google.com/drive/api/v3/reference/files/list
该工具使用与我的个人Google账号关联的OAuth 2.0,而不是服务账号,但也失败了。

英文:

I am using a service account to connect to a shared drive in my personal Google account. The Google Drive API always returns an error saying the shared drive was not found. I tried both of these:

  • making the shared drive public for anyone with the link
  • adding permission for a specific user (the service account) using the service account's email address

The link for the shared drive is in this format https://drive.google.com/drive/folders/xyz
and I assume the driveId is the last part of the link, xyz? Or is that the folder id? If so then how do I find the driveId?

  1. // load the service account credentials
  2. data, err := ioutil.ReadFile("service-account.json")
  3. if err != nil {
  4. log.Fatal("failed to read json file")
  5. }
  6. // parse the credentials file
  7. conf, err := google.JWTConfigFromJSON(data, drive.DriveReadonlyScope)
  8. if err != nil {
  9. log.Fatal("failed to parse json file")
  10. }
  11. apiKeyBytes, err := ioutil.ReadFile("api-key.txt")
  12. API_KEY := string(apiKeyBytes)
  13. DRIVE_ID := "1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
  14. // send the GET request with all the parameters
  15. client := conf.Client(context.Background())
  16. parameters := "?key=" + API_KEY
  17. parameters += "&corpora=drive"
  18. parameters += "&includeItemsFromAllDrives=true"
  19. parameters += "&supportsAllDrives=true"
  20. parameters += "&driveId=" + DRIVE_ID
  21. response, err := client.Get("https://www.googleapis.com/drive/v3/files" + parameters)
  22. // read and print the response
  23. data_buffer := make([]byte, 2048)
  24. _, err = response.Body.Read(data_buffer)
  25. response.Body.Close()
  26. fmt.Println(string(data_buffer))

Here is the output when this program is run:

  1. {
  2. "error": {
  3. "errors": [
  4. {
  5. "domain": "global",
  6. "reason": "notFound",
  7. "message": "Shared drive not found: 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm",
  8. "locationType": "parameter",
  9. "location": "driveId"
  10. }
  11. ],
  12. "code": 404,
  13. "message": "Shared drive not found: 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
  14. }
  15. }

I also tried the "Try this API" tool at this link https://developers.google.com/drive/api/v3/reference/files/list
which was using OAuth 2.0 tied to my personal Google account instead of the service account, and that failed too.

答案1

得分: 7

当我看到你的示例URL https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm 时,我认为在这种情况下,这是一个公开共享的文件夹。我认为这可能是你遇到问题的原因。在这种情况下,以下修改如何?

从:

  1. parameters := "?key=" + API_KEY
  2. parameters += "&corpora=drive"
  3. parameters += "&includeItemsFromAllDrives=true"
  4. parameters += "&supportsAllDrives=true"
  5. parameters += "&driveId=" + DRIVE_ID

到:

  1. parameters := "?key=" + API_KEY
  2. parameters := "&q='1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm' in parents"
  • q 的值是 '1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm' in parents
  • 当你的API密钥是有效的密钥时,可以使用这个修改。如果出现错误,请使用 "Try this API" 进行测试。参考

注意:

  • 当检索共享驱动器的文件夹的元数据时,driveId 的值包含在返回的值中。当我测试你的文件夹ID时,元数据中没有包含这个值。所以我认为你的文件夹可能是 Google Drive 的公开共享文件夹,而不是共享驱动器。

参考:

英文:

When I saw your sample URL of https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm, I thought that in this case, it's a publicly shared folder. I thought that this might be the reason for your issue. In this case, how about the following modification?

From:

  1. parameters := "?key=" + API_KEY
  2. parameters += "&corpora=drive"
  3. parameters += "&includeItemsFromAllDrives=true"
  4. parameters += "&supportsAllDrives=true"
  5. parameters += "&driveId=" + DRIVE_ID

To:

  1. parameters := "?key=" + API_KEY
  2. parameters := "&q=%271dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm%27%20in%20parents"
  • The value of q is '1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm' in parents.
  • When your API key is valid key, this modification can be used. If an error occurs, please test this with "Try this API". Ref

Note:

  • When the metadata of the folder of the shared Drive is retrieved, the value of driveId is included in the returned value. When I tested your folder ID, this value was not included in the metadata. So I thought that your folder might be the publicly shared folder of Google Drive which is not the shared Drive.

References:

huangapple
  • 本文由 发表于 2022年1月18日 08:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/70749061.html
匿名

发表评论

匿名网友

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

确定