问题:gspread认证抛出权限不足

使用 developers.google.com 我们创建了 api 用户并将凭据下载为 json 文件。现在在我的 macbook 上,gspread 身份验证在使用 credentials.json 时工作正常。当将相同的配置移动到 aws 上的 linux 服务器时,它会给出 403 权限不足错误。

Pip 和 python 版本相同。

例外

gspread.v4.exceptions.APIError: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

基本代码

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(creds)

sheet = client.open('MySheetName').sheet1

解答

尝试将您的scope变量更改为以下内容:

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

确保在 API 控制台中启用了 Drive API。

gspread 已升级,现在基于 API v4。它更快,但需要范围内的更新。

这是同样的问题:https://github.com/burnash/gspread/issues/512

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐