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

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

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?

// 加载服务账号凭据
data, err := ioutil.ReadFile("service-account.json")
if err != nil {
	log.Fatal("无法读取JSON文件")
}

// 解析凭据文件
conf, err := google.JWTConfigFromJSON(data, drive.DriveReadonlyScope)
if err != nil {
	log.Fatal("无法解析JSON文件")
}

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

// 使用所有参数发送GET请求
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)

// 读取并打印响应
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": "找不到共享驱动:1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm",
        "locationType": "parameter",
        "location": "driveId"
      }
    ],
    "code": 404,
    "message": "找不到共享驱动:1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
  }
}

我还尝试了此链接上的“尝试此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?

// 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))

Here is the output when this program is run:

{
 "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"
 }
}

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 时,我认为在这种情况下,这是一个公开共享的文件夹。我认为这可能是你遇到问题的原因。在这种情况下,以下修改如何?

从:

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

到:

parameters := "?key=" + API_KEY
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:

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

To:

parameters := "?key=" + API_KEY
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:

确定