【问题标题】:'str' does not support the buffer interface Python 3 error for oauth'str' 不支持 oauth 的缓冲区接口 Python 3 错误
【发布时间】:2015-07-09 06:37:11
【问题描述】:
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() 不支持这种类型。这是[文档] [1],显然没有多大帮助。 我正在使用 Python 3.4。我认为文档代码仅适用于 3 之前的版本。

编辑:

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

Traceback(最近一次调用最后一次):文件 “C:\Users\sony\Desktop\Python\new.py”,第 9 行,在 凭据 = SignedJwtAssertionCredentials.from_json(json_key['client_email'], json_key['private_key'], scope) TypeError: from_json() 需要 2 位置参数,但给出了 4 个 [在 1.0 秒内完成,退出代码 1]

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

credentials = SignedJwtAssertionCredentials.from_json(json_key)

Traceback(最近一次调用最后一次):文件 “C:\Users\sony\Desktop\Python\new.py”,第 8 行,在 凭据 = SignedJwtAssertionCredentials.from_json(json_key) 文件“C:\Python34\lib\site-packages\oauth2client\client.py”,行 第1479章 数据 = json.loads(s) 文件“C:\Python34\lib\json__init__.py”,第 312 行,加载中 s.class.name)) TypeError: JSON object must be str, not 'dict'

【问题讨论】:

标签: python google-spreadsheet-api gspread


【解决方案1】:

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

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

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

【讨论】:

  • "cast" 非常不准确。您必须使用编码(如何将代码点表示为字节的标准)将 encode 字符串(一系列 unicode 代码点)转换为 bytes
【解决方案2】:

这个错误报告中有一个解决方案,但我自己没有尝试过。 https://github.com/burnash/gspread/issues/224

【讨论】:

    猜你喜欢
    • 2015-08-08
    • 2014-08-09
    • 2023-03-10
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    相关资源
    最近更新 更多