【问题标题】:python + google sheets == gspread, but sadly AttributeError: type object 'ServiceAccountCredentials' has no attribute 'from_json_keyfile'python + google sheet == gspread,但遗憾的是AttributeError:类型对象'ServiceAccountCredentials'没有属性'from_json_keyfile'
【发布时间】:2019-06-25 02:39:50
【问题描述】:

我正在尝试使用 gspread 库将 google 表格与 python 连接起来。

我已经尝试过大量使用 gspread 的示例,但无济于事。我也使用过同样令人失望和绝望的谷歌官方图书馆(实际上谷歌代码导致终端下的身份验证页面不起作用“因为你的浏览器中没有启用javascript”)

我的示例代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

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

credentials = ServiceAccountCredentials.from_json_keyfile.name('client_secret.json', scope)
gc = gspread.authorize(credentials)
hoja = gc.open('testpy').sheet1
print(hoja.get_all_records())

client.secret.json 是从从服务帐户凭据获得的密钥 json 文件重命名的。是这样的

{
"type": "service_account",
"project_id": "personaltestpygshee6",
"private_key_id": "29etcetc",
"private_key": "-----BEGIN PRIVATE KEY-----\netcetc\n-----END PRIVATE KEY-----\n",
"client_email": "project@project.iam.gserviceaccount.com",
"client_id": 423etcetc",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/project%40project.iam.gserviceaccount.com"
}

当我运行脚本时,这是我得到的错误消息

File "5testgooshee.py", line 6, in <module>
credentials = ServiceAccountCredentials.from_json_keyfile.name('client_secret.json', scope)
AttributeError: type object 'ServiceAccountCredentials' has no attribute 'from_json_keyfile'

我已经完成了我的作业,我发誓。蒸汽从我的耳朵里冒出来。我无法弄清楚问题是什么以及为什么它在除我之外的许多示例中都有效

失败在哪里?为什么我不能让它工作?

提前致谢

【问题讨论】:

  • 放弃已弃用的oauth2client。 google-auth 库面向服务帐户授权。
  • 如果您的脚本被使用,请将ServiceAccountCredentials.from_json_keyfile.name('client_secret.json', scope)修改为ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope),然后重试。
  • 1.当我测试帐户授权时,终端中出现了似乎是 google 帐户登录的内容,例如文本浏览器。当我填写这些字段时,服务回答说我的浏览器中没有授权 javascript,我无法超越这一点。此外,gspread 被很多人使用,而且他们似乎在使用它时不连贯:(
  • 谢谢,@tanaike
  • @Juan Luis Chulilla 感谢您的回复。从报错信息中发现脚本有效。在我的环境中,脚本有效。但是关于Invalid JWT Signature.,这可能对您的情况有用。 stackoverflow.com/questions/37710245/…

标签: python google-api google-oauth google-sheets-api gspread


【解决方案1】:

这个问题终于有了(临时的?)解决方案。我们需要记住,OAuth2 已被弃用,所有基于它的解决方案(例如 GSpread)都可能停止运行。

现在,解决方案:

  1. 正如@Tanaike 指出的那样,起点是调用服务帐户凭据时的拼写错误。需要指出的是,在一些tutos中它被拼写为ServiceAccountCredentials.from_json_keyfile.name('client_secret.json', scope),这里应该是ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)。那里有教程复制了这个错字。
  2. 此外,Windows 还存在另一个问题。您需要使用 python 安装PyOpenSSL。如果你使用windows box或者WSL,需要安装完整版的win32OpenSSL(来源:ImportError: cannot import name SignedJwtAssertionCredentialshttp://slproweb.com/products/Win32OpenSSL.html需要在PyOpenSSL之前安装... 我发现你有安装与 python 相同的版本,32 位或 64 位。这听起来可能很愚蠢或微不足道,但如果你不考虑它,它就行不通

感谢大家的时间和回答

【讨论】:

    猜你喜欢
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多