【问题标题】:gspread authentication throwing insufficient permissiongspread 身份验证抛出权限不足
【发布时间】:2018-08-21 20:24:54
【问题描述】:

使用 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

【问题讨论】:

  • 我在我的两台 linux 服务器中的一台上看到了类似的问题。我目前在我的服务器之间唯一的区别是他们安装了什么版本的 gspread,也许检查一下你的两台机器正在使用什么版本的 gspread?请告诉我你是如何解决的!
  • 我在我的 linux 服务器上回滚了 gspread 的版本,它遇到了同样的问题,这似乎已经解决了我的问题,暗示 gspread 2.0 可能是罪魁祸首,希望你是一个类似的问题.这就是我运行的: pip install 'gspread==0.6.2' --force-reinstall

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


【解决方案1】:

尝试将您的 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

【讨论】:

  • 为我工作!起初它给了我“权限被拒绝”和一个激活第一次执行的链接。之后它起作用了。
【解决方案2】:

您似乎正在使用服务帐户。为了让服务帐户能够访问您的电子表格,它需要能够访问它。

确保您与服务帐户电子邮件地址共享工作表,您可以通过谷歌驱动器网页做到这一点

【讨论】:

  • 我不认为这可能是问题,OP 说服务帐户在 Macbook 上工作,但在 linux 服务器上不工作,所以它必须具有访问权限。
【解决方案3】:

如果更新范围后仍无法访问,请尝试在终端中运行此命令:

sudo pip install 'gspread==0.6.2' --force-reinstall

【讨论】:

  • 我不鼓励人们降级:gspread 0.6.2 使用旧版本的 Sheets API (v3)。 Google 强烈建议从 v3 升级,因为它将被弃用。见developers.google.com/sheets/api/v3
猜你喜欢
  • 2019-10-02
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2012-02-18
相关资源
最近更新 更多