'str'不支持oauth的缓冲区接口Python 3错误

Abh*_*tia 5 python google-spreadsheet-api gspread

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)
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

我尝试使用编码字符串,.encode()gspread.authorize()不支持这种类型.这是[文档] [1],显然没什么帮助.我使用的是Python 3.4.我认为文档代码仅适用于3之前的版本.

编辑:

credentials = SignedJwtAssertionCredentials.from_json(json_key['client_email'], json_key['private_key'], scope)
Run Code Online (Sandbox Code Playgroud)

回溯(最近一次调用最后一次):文件"C:\ Users\sony\Desktop\Python \new.py",第9行,在凭证= SignedJwtAssertionCredentials.from_json(json_key ['client_email'],json_key ['private_key'],范围)TypeError:from_json()需要2个位置参数但是给出了4个[在1.0s内完成,退出代码为1]

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

credentials = SignedJwtAssertionCredentials.from_json(json_key)
Run Code Online (Sandbox Code Playgroud)

回溯(最近一次调用最后一次):文件"C:\ Users\sony\Desktop\Python \new.py",第8行,在credentials = SignedJwtAssertionCredentials.from_json(json_key)文件"C:\ Python34\lib\site-packages\oauth2client\client.py",第1479行,in_json data = json.loads(s)文件"C:\ Python34\lib\json__init __.py",第312行,在加载s中.上课.name))TypeError:JSON对象必须是str,而不是'dict'

two*_*rec 5

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

这是一个适合我的简单解决方案:

credentials = SignedJwtAssertionCredentials(
            json_key['client_email']
            , bytes(json_key['private_key'], 'UTF-8')
            , scope)
Run Code Online (Sandbox Code Playgroud)