我正在使用服務帳戶連接到我個人 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 ,這也失敗了。
uj5u.com熱心網友回復:
當我看到您的示例 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”進行測驗。參考
筆記:
- 檢索共享 Drive 檔案夾的元資料時,
driveId回傳值中包含 的值。當我測驗您的檔案夾 ID 時,此值未包含在元資料中。所以我認為您的檔案夾可能是 Google Drive 的公開共享檔案夾,而不是共享 Drive。
參考:
- 檔案:串列
- 搜索檔案和檔案夾
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/414523.html
標籤:
