蟒+谷歌片== gspread,但遗憾的是AttributeError的:对象类型“ServiceAccountCredentials”没有属性“from_json_keyfile”

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

我想谷歌床单使用gspread库蟒蛇连接。

我已经尝试了极大的相集使用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())

qazxsw POI是从服务帐户凭据获得密钥JSON文件重命名。它是这样的

client.secret.json

当我运行该脚本,这是我得到错误信息

{
"type": "service_account",
"project_id": "personaltestpygshee6",
"private_key_id": "29etcetc",
"private_key": "-----BEGIN PRIVATE KEY-----\netcetc\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"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"
}

我已经做了功课,我发誓。蒸出来的我的耳朵。我想不出有什么问题,为什么它工作在很多例子矿井除外

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

提前致谢

python google-api google-oauth google-sheets-api gspread
1个回答
0
投票

最后是对这个问题的(暂时的?)解决方案。我们需要记住的OAuth2已被弃用,并且在此基础上,如GSpread所有的解决方案可以停止工作。

现在,该解决方案:

  1. 作为@Tanaike指出的,出发点是对的服务帐户凭据调用一个错字。它需要指出的是,在一些TUTOS它被拼写为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' ,它应该是ServiceAccountCredentials.from_json_keyfile.name('client_secret.json', scope)。那边有教程在此错字转载。
  2. 此外,还有与Windows的另一个问题。你需要使用python安装ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)。如果您使用的是Windows中或WSL,您需要安装PyOpenSSL(来源:win32OpenSSL)的完整版本ImportError: cannot import name SignedJwtAssertionCredentials您需要PyOpenSSL之前安装了它,而且我发现,您必须安装相同版本的蟒蛇,无论是32或64位。这听起来愚蠢或琐碎的,但如果你不考虑它,它不会工作

感谢所有为您的时间和答案

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