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)
回溯(最近一次通话最后一次):文件“C:\Users\sony\Desktop\Python\new.py”,第 9 行,凭据 = SignedJwtAssertionCredentials.from_json(json_key['client_email'], json_key['private_key'],范围)类型错误:from_json() 接受 2 个位置参数,但给出了 4 个 [在 1.0 秒内完成,退出代码为 1]
credentials = SignedJwtAssertionCredentials.from_json(json_key)
回溯(最后一次调用):文件“C:\Users\sony\Desktop\Python\new.py”,第 8 行,凭据 = SignedJwtAssertionCredentials.from_json(json_key) 文件“C:\Python34\lib\site-packages \oauth2client\client.py”,第 1479 行,在 from_json 数据 = json.loads(s) 文件“C:\Python34\lib\json__init__.py”,第 312 行,在负载 s 中。类。name )) TypeError: JSON 对象必须是 str,而不是 'dict'