【发布时间】: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