使用Python API和ADC读取谷歌表格遇403权限不足问题排查
Google Sheets API 403权限问题
我编写了以下代码:
scope = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/spreadsheets.readonly'] credentials, project = google.auth.default(scopes=scope) service = discovery.build('sheets', 'v4', credentials=credentials, cache_discovery=False) sheet = service.spreadsheets() result_input = sheet.values().get(spreadsheetId=id,range=range).execute()
但即便表格已设为公开,我仍收到403错误:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/<SHEET_ID_HERE>/values/<RANGE_HERE>?alt=json returned "Request had insufficient authentication scopes.". Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'service': 'sheets.googleapis.com', 'method': 'google.apps.sheets.v4.SpreadsheetsService.GetValues'}}]">
已完成以下配置:
- Google SDK配置正常
- 我是该GCP项目的所有者
- 我是该谷歌表格的所有者,且邮箱一致(表格已设为公开)
我不想下载任何.json格式的API密钥,是否有其他解决方案?我哪里操作出错了?
补充说明
似乎无法使用ADC(应用默认凭据)访问Google Sheets API,必须通过服务账号或Oauth2配置实现。
内容的提问来源于stack exchange,提问作者Matheus Oliveira
相关产品推荐
相关产品推荐

