gpt4 book ai didi

python - 'str' 不支持 oauth 的缓冲区接口(interface) Python 3 错误

转载 作者:行者123 更新时间:2023-12-04 18:39:19 24 4
gpt4 key购买 nike

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open('Crowds-9569176f5988.json'))
scope = ['https://spreadsheets.google.com/feeds']

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
#gc = gspread.authorize(credentials)

错误:
Traceback (most recent call last):   File "C:\Users\sony\Desktop\Python\new.py", line 8, in <module>
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope) File "C:\Python34\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs) File "C:\Python34\lib\site-packages\oauth2client\client.py", line 1469, in
__init__
self.private_key = base64.b64encode(private_key) File "C:\Python34\lib\base64.py", line 62, in b64encode
encoded = binascii.b2a_base64(s)[:-1] TypeError: 'str' does not support the buffer interface

我尝试使用 .encode() 对字符串进行编码但是 gspread.authorize()不支持这种类型。这是 [documentation][1],显然没有多大帮助。
我正在使用 Python 3.4。我认为文档代码仅适用于 3 之前的版本。

编辑:
credentials = SignedJwtAssertionCredentials.from_json(json_key['client_email'], json_key['private_key'], scope)

Traceback (most recent call last): File "C:\Users\sony\Desktop\Python\new.py", line 9, in credentials = SignedJwtAssertionCredentials.from_json(json_key['client_email'], json_key['private_key'], scope) TypeError: from_json() takes 2 positional arguments but 4 were given [Finished in 1.0s with exit code 1]

[1]: http://gspread.readthedocs.org/en/latest/oauth2.html


credentials = SignedJwtAssertionCredentials.from_json(json_key)

Traceback (most recent call last): File "C:\Users\sony\Desktop\Python\new.py", line 8, in credentials = SignedJwtAssertionCredentials.from_json(json_key) File "C:\Python34\lib\site-packages\oauth2client\client.py", line 1479, in from_json data = json.loads(s) File "C:\Python34\lib\json__init__.py", line 312, in loads s.class.name)) TypeError: the JSON object must be str, not 'dict'

最佳答案

您正在使用 Python3.x,其中 string与 Python 2.x 的类型不同。您需要将其转换为字节(对其进行编码)。

这是对我很有效的简单解决方案:

credentials = SignedJwtAssertionCredentials(
json_key['client_email']
, bytes(json_key['private_key'], 'UTF-8')
, scope)

关于python - 'str' 不支持 oauth 的缓冲区接口(interface) Python 3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29952828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com