2015-04-29 19 views
5
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) 

Lỗi:'str' không hỗ trợ giao diện đệm lỗi Python 3 cho oauth

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 

tôi đã cố gắng sử dụng mã hóa chuỗi .encode() nhưng gspread.authorize() không hỗ trợ một kiểu như vậy. Đây là [tài liệu] [1] mà dường như không có nhiều trợ giúp. Tôi đang sử dụng Python 3.4. Tôi nghĩ rằng các mã tài liệu chỉ hoạt động cho phiên bản trước 3.

EDIT:

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

Traceback (most recent call last): File "C:\Users\sony\Desktop\Python\new.py", line 9, in credentials = SignedJwtAssertionCredentials.from_json(json_key['client_email'], json_key['private_key'], scope) TypeError: from_json() takes 2 positional arguments but 4 were given [Finished in 1.0s with exit code 1]

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

credentials = SignedJwtAssertionCredentials.from_json(json_key) 

Traceback (most recent call last): File "C:\Users\sony\Desktop\Python\new.py", line 8, in credentials = SignedJwtAssertionCredentials.from_json(json_key) File "C:\Python34\lib\site-packages\oauth2client\client.py", line 1479, in from_json data = json.loads(s) File "C:\Python34\lib\json__init__.py", line 312, in loads s.class.name)) TypeError: the JSON object must be str, not 'dict'

+1

OAuth2 và bảng tính-api với python https://github.com/asm-products/gridspree - nó hoạt động – eddyparkinson

Trả lời

5

Bạn đang sử dụng Python3.x, trong đó string không phải là loại tương tự như đối với Python 2.x. Bạn cần phải truyền nó vào byte (mã hóa nó).

Đây là giải pháp đơn giản mà làm việc tốt cho tôi:

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

"diễn viên" là rất không chính xác . Bạn phải 'chuỗi mã hóa '(một chuỗi các điểm mã hóa unicode) thành' byte' bằng cách sử dụng một mã hóa (một tiêu chuẩn cho cách biểu diễn các điểm mã hóa dưới dạng byte) – Daenyth

Các vấn đề liên quan