我想谷歌床单使用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"
}
我已经做了功课,我发誓。蒸出来的我的耳朵。我想不出有什么问题,为什么它工作在很多例子矿井除外
在哪里失败?为什么我不能使它工作吗?
提前致谢
最后是对这个问题的(暂时的?)解决方案。我们需要记住的OAuth2已被弃用,并且在此基础上,如GSpread所有的解决方案可以停止工作。
现在,该解决方案:
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)
。那边有教程在此错字转载。ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
。如果您使用的是Windows中或WSL,您需要安装PyOpenSSL
(来源:win32OpenSSL
)的完整版本ImportError: cannot import name SignedJwtAssertionCredentials您需要PyOpenSSL之前安装了它,而且我发现,您必须安装相同版本的蟒蛇,无论是32或64位。这听起来愚蠢或琐碎的,但如果你不考虑它,它不会工作感谢所有为您的时间和答案