【问题标题】:Google Drive API error - "message": "Shared drive not found: xyz"Google Drive API 错误 - “消息”:“找不到共享驱动器:xyz”
【发布时间】:2022-01-18 10:07:47
【问题描述】:

我正在使用服务帐户连接到我个人 Google 帐户中的共享云端硬盘。 Google Drive API 总是返回一个错误,指出找不到共享驱动器。这两个我都试过了:

  • 向知道链接的任何人公开共享云端硬盘
  • 使用服务帐户的电子邮件地址为特定用户(服务帐户)添加权限

共享云端硬盘的链接格式为https://drive.google.com/drive/folders/xyz 我假设 driveId 是链接的最后一部分,xyz?或者是文件夹ID?如果是这样,那么我如何找到 driveId?

// load the service account credentials
data, err := ioutil.ReadFile("service-account.json")
if err != nil {
    log.Fatal("failed to read json file")
}

// parse the credentials file
conf, err := google.JWTConfigFromJSON(data, drive.DriveReadonlyScope)
if err != nil {
    log.Fatal("failed to parse json file")
}

apiKeyBytes, err := ioutil.ReadFile("api-key.txt")
API_KEY := string(apiKeyBytes)
DRIVE_ID := "1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"

// send the GET request with all the parameters
client := conf.Client(context.Background())
parameters := "?key=" + API_KEY
parameters += "&corpora=drive"
parameters += "&includeItemsFromAllDrives=true"
parameters += "&supportsAllDrives=true"
parameters += "&driveId=" + DRIVE_ID
response, err := client.Get("https://www.googleapis.com/drive/v3/files" + parameters)

// read and print the response
data_buffer := make([]byte, 2048)
_, err = response.Body.Read(data_buffer)
response.Body.Close()
fmt.Println(string(data_buffer))

这是程序运行时的输出:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Shared drive not found: 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm",
    "locationType": "parameter",
    "location": "driveId"
   }
  ],
  "code": 404,
  "message": "Shared drive not found: 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
 }
}

我还在此链接https://developers.google.com/drive/api/v3/reference/files/list 上尝试了“试用此 API”工具 它使用绑定到我的个人 Google 帐户而不是服务帐户的 OAuth 2.0,但也失败了。

【问题讨论】:

  • 我认为当查询参数中的这些值是有效值时,您的请求有效。那么根据你的错误信息和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.,再次确认驱动ID和共享驱动的权限如何?
  • @Tanaike 我刚刚在https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm 创建了一个公开可用的测试共享驱动器。当我在代码中将 driveId 用作 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm 时,我仍然收到错误消息。
  • 感谢您的回复。从您的回复中,我提出了一个修改点作为答案。你能确认一下吗?如果这没有用,我深表歉意。

标签: go google-drive-api


【解决方案1】:

当我看到您的示例 URL https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm 时,我认为在这种情况下,它是一个公共共享文件夹。我认为这可能是您的问题的原因。既然如此,那么下面的修改呢?

发件人:

parameters := "?key=" + API_KEY
parameters += "&corpora=drive"
parameters += "&includeItemsFromAllDrives=true"
parameters += "&supportsAllDrives=true"
parameters += "&driveId=" + DRIVE_ID

收件人:

parameters := "?key=" + API_KEY
parameters := "&q=%271dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm%27%20in%20parents"
  • q 的值为'1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm' in parents
  • 当您的 API 密钥是有效密钥时,可以使用此修改。如果发生错误,请使用“Try this API”进行测试。 Ref

注意:

  • 检索共享Drive文件夹的元数据时,返回值中包含driveId的值。当我测试您的文件夹 ID 时,此值未包含在元数据中。所以我认为您的文件夹可能是 Google Drive 的公开共享文件夹,而不是共享 Drive。

参考资料:

【讨论】:

  • 谢谢,成功了!我想我将共享驱动器与共享文件夹混淆了。
  • @Justice Oarry 感谢您的回复和测试。我很高兴你的问题得到了解决。我可以从您提供的示例 URL 中理解。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 2021-07-26
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2020-04-11
相关资源
最近更新 更多