尝试访问Google表格时出现“无访问令牌响应”错误

时间:2020-10-12 11:07:38

标签: python google-drive-api google-api-python-client gspread

我试图通过使用Python处理Google Sheet

遵循此站点的指导: https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html 包括步骤:

  1. 转到Google API控制台。

  2. 创建一个新项目。

  3. 单击“启用API”。搜索并启用Google Drive API。

  4. 为Web服务器创建凭据以访问应用程序数据。

  5. 命名服务帐户并授予其“编辑者”的项目角色。

  6. 下载JSON文件。

  7. 将JSON文件复制到您的代码目录中,并将其重命名为 client_secret.json

这是我到目前为止所做的

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://docs.google.com/spreadsheets/d/1IypqDnLKKR-IX1oOwCuTejBQ-RFq87K9rqSF6GpSyn4/edit?usp=drive_web&ouid=109125393303568297837']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open('test_xlsx').sheet1

但是,我收到此结果错误

google.auth.exceptions.RefreshError :(“响应中没有访问令牌。”, {'id_token'..}

2 个答案:

答案 0 :(得分:2)

那不是Google范围。这是有效的Google scopes#sheets

的列表

即使您正在遵循的教程也指出要使用以下范围。在不包含正确的oauth范围的情况下,您的字体可以访问。

scope = ['https://spreadsheets.google.com/feeds']

答案 1 :(得分:1)

我在使用教程中的范围时遇到了同样的错误,所以我尝试了 DaImTo 建议的 scopes#sheets 中的几个,直到它最终起作用。 对我来说是scope = ['https://www.googleapis.com/auth/drive']