我正在创建需要读取一堆电子表格的服务器应用程序。因此,我们使用中的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)
我在正确的轨道上,或我只是失去了一些东西很愚蠢?