【发布时间】:2020-08-08 06:27:41
【问题描述】:
我正在尝试从 Python(在 GKE 中运行)访问 Google Sheet(只读模式)。
我能够获得应用程序默认凭据,但遇到范围问题(因为我缺少 https://www.googleapis.com/auth/spreadsheets.readonly 范围)。见以下代码:
from googleapiclient.discovery import build
from oauth2client import client
creds=client.GoogleCredentials.get_application_default()
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
sheet.values().get(spreadsheetId='XXXXXXXXXX', range='Sheet1!A:C').execute()
输出是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 840, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/XXXXXX/values/Sheet1%21A%3AC?alt=json returned "Request had insufficient authentication scopes.">
我尝试阅读任何可用的文档,但所有相关范围都在使用外部服务帐户 JSON 文件。
有没有办法使用 Application Default 并添加所需的范围?
【问题讨论】:
标签: google-sheets-api google-oauth scopes