我正在創建需要讀取一堆電子表格的服務器應用程序。因此,我們使用中的GoogleCredentials.get_application_default()
,因爲服務器可以訪問服務帳戶。對於本地開發,我想使用我的個人憑據,所以我正在使用Google Cloud SDK。我使用gcloud auth application-default login
來確保工作。Google表格+ GoogleCredentials =「身份驗證範圍不足」
創建服務是如此的簡單:
from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build
credentials = GoogleCredentials.get_application_default()
service = build('sheets', 'v4', credentials=credentials)
而事實上,這一直很好,到目前爲止,對於一幫谷歌的雲產品。 然而,使用表API,當我執行任何請求時,出現以下錯誤:
googleapiclient.errors.HttpError:
<HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/...
returned "Request had insufficient authentication scopes.">
這隻能與谷歌API表時,其他API(如BigQuery的V2 API)工作得很好。
我已經檢查:
- 牀單API已在谷歌雲項目啓用
- 我可以訪問電子表格和編輯權限的谷歌雲項目
我試着創建範圍憑證的各種方法,如:
credentials = GoogleCredentials.get_application_default() scoped_creds = credentials.create_scoped(SCOPES)
但是,這似乎沒有任何影響,因爲e
create_scoped
方法只是返回自我(source)。- 我在該項目上創建了一個服務帳戶,並將環境變量
GOOGLE_APPLICATION_CREDENTIALS
設置爲指向它。這也可以正常工作,可能是因爲它使用ServiceAccountCredentials
這實際上有一個create_scoped
方法的實現。 - 我使用的是最新版本的谷歌的API的Python客戶端的(1.6.2)和oauth2client(4.0.0)
我在正確的軌道上,或我只是失去了一些東西很愚蠢?