我正在使用服務帳戶連接到我個人 Google 帳戶中的共享驅動器。Google Drive API 總是返回一個錯誤,指出找不到共享驅動器。我嘗試了這兩個:向知道鏈接的任何人公開共享云端硬盤使用服務帳戶的電子郵件地址為特定用戶(服務帳戶)添加權限共享驅動器的鏈接采用這種格式https://drive.google.com/drive/folders/xyz 我假設 driveId 是鏈接的最后一部分,xyz?還是那個文件夾ID?如果是這樣,那么我如何找到 driveId?// load the service account credentialsdata, err := ioutil.ReadFile("service-account.json")if err != nil { log.Fatal("failed to read json file")}// parse the credentials fileconf, 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 parametersclient := conf.Client(context.Background())parameters := "?key=" + API_KEYparameters += "&corpora=drive"parameters += "&includeItemsFromAllDrives=true"parameters += "&supportsAllDrives=true"parameters += "&driveId=" + DRIVE_IDresponse, err := client.Get("https://www.googleapis.com/drive/v3/files" + parameters)// read and print the responsedata_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 ,這也失敗了。
1 回答

catspeake
TA貢獻1111條經驗 獲得超0個贊
當我看到您的示例 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”進行測試。參考
筆記:
檢索共享 Drive 文件夾的元數據時,
driveId
返回值中包含 的值。當我測試您的文件夾 ID 時,此值未包含在元數據中。所以我認為您的文件夾可能是 Google Drive 的公開共享文件夾,而不是共享 Drive。
- 1 回答
- 0 關注
- 154 瀏覽
添加回答
舉報
0/150
提交
取消