1

我正在尝试将 Google 电子表格 OAuth 与 gspread 库一起使用。发送凭据时出现TypeError: expected bytes, not str异常。我该如何解决?

import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open('Test for gspread-78c678a7fb15.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 "<pyshell#27>", line 1, in <module>
    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
  File "C:\Python33\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Python33\lib\site-packages\oauth2client\client.py", line 1515, in __init__
    self.private_key = base64.b64encode(private_key)
  File "C:\Python33\lib\base64.py", line 58, in b64encode
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
4

1 回答 1

1

密钥是一个字符串,需要编码为字节。替换credentials =为以下内容:

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
于 2015-05-27T15:02:51.607 回答