我的 Python 脚本无法访问我的 Google 表格

问题描述 投票:0回答:1

我的 Python 脚本无法访问我的 Google 表格,因为我收到此错误:

发生异常:RefreshError ('invalid_grant: 无效的 JWT 签名。', {'error': 'invalid_grant', 'error_description': '无效的 JWT 签名。'})

我在 Google Cloud Console 中执行了以下操作: 我启用了云端硬盘 API 我启用了 Sheets API 我为两个 API 创建了凭据,在其中创建了服务帐户,并将角色设置为编辑者 我在 JSON 文件中创建了一个私钥,并将其重命名为 client_secret.json 并在下面的脚本中使用它。 我打开了 JSON 文件,并使用分配给“client_email”的值将我的 Google 表格共享到具有编辑器访问权限的该地址。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

sheet = client.open('Add stock').sheet1

clientlist = sheet.get_all_records()
print(clientlist)
python google-sheets google-cloud-platform
1个回答
0
投票

建议:生成新的服务密钥

如果服务帐户密钥无效或服务帐户本身被禁用,我可以复制您的错误。由于根据您所执行的步骤的详细分类,后者可能不适用,因此无效的服务帐户密钥是最可能的原因。我建议删除您现有的私钥并创建一个新的私钥,这将生成一个新的 JSON 文件用于身份验证。

此外,我建议将您的范围更改为:

scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']

仅使用

https://spreadsheets.google.com/feeds
会引发
Request had insufficient authentication scope
错误。

© www.soinside.com 2019 - 2024. All rights reserved.