![](/img/trans.png)
[英]Python TypeError - Expected bytes but got 'str' when trying to created signature
[英]gspread gives TypeError: expected bytes when trying to log in
我正在嘗試將Google Spreadsheets OAuth與gspread庫一起使用。 我收到TypeError: expected bytes, not str
發送證書時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
密鑰是一個字符串,需要編碼為字節。 將credentials =
替換為以下內容:
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.