使用 OAuth2 在使用 python 的 gspread 中授权的问题
Issues using OAuth2 to authorize in gspread using python
我对 python 很陌生,所以请原谅我的无知。
我正在尝试将数据发送到 Google 电子表格并决定使用 gspread.
但是 gspread 要求我使用 OAuth-2.0 授权访问电子表格。我在他们的文档页面上使用了 tutorial 来这样做。但是当我执行我的代码时:
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open('test IGS-1859066a1c38.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['760 ... 6r9@developer.gserviceaccount.com'], json_key['-----BEGIN PRIVATE KEY-----\nMI ... 003d\n-----END PRIVATE KEY-----\n'], scope)
gc = gspread.authorize(credentials)
wks = gc.open("ITGS_TEST").sheet1
我收到以下错误:
KeyError: '760 ... 6r9@developer.gserviceaccount.com'
我不知道如何解决这个问题,也看不出我遗漏了什么。如果有人可以提供他们的意见,我将不胜感激。
提前致谢
编辑:
我听从了 Sandeep107 的建议并将我的代码改回:
SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
我不再收到旧的错误代码,而是收到以下错误:
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
File "F:\Python33\lib\site-packages\oauth2client-1.4.7-py3.3.egg\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "F:\Python33\lib\site-packages\oauth2client-1.4.7-py3.3.egg\oauth2client\client.py", line 1487, in __init__
self.private_key = base64.b64encode(private_key)
File "F:\Python33\lib\base64.py", line 58, in b64encode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
再次感谢您的帮助。
检查link:http://gspread.readthedocs.org/en/latest/oauth2.html
我认为你需要 "client_email." 看起来你可能 "client_key" 两次。
告诉我你是怎么做出来的。我正在尝试自己解决整个 oauth2 问题,但进展并不顺利。
您应该按原样使用json_key['client_email']、json_key['private_key']。
无需替换为实际值
Python 3 要求将字符串转换为字节,Python 2.x 使用字符串,您使用的是哪个 Python 版本?
我对 python 很陌生,所以请原谅我的无知。
我正在尝试将数据发送到 Google 电子表格并决定使用 gspread.
但是 gspread 要求我使用 OAuth-2.0 授权访问电子表格。我在他们的文档页面上使用了 tutorial 来这样做。但是当我执行我的代码时:
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open('test IGS-1859066a1c38.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['760 ... 6r9@developer.gserviceaccount.com'], json_key['-----BEGIN PRIVATE KEY-----\nMI ... 003d\n-----END PRIVATE KEY-----\n'], scope)
gc = gspread.authorize(credentials)
wks = gc.open("ITGS_TEST").sheet1
我收到以下错误:
KeyError: '760 ... 6r9@developer.gserviceaccount.com'
我不知道如何解决这个问题,也看不出我遗漏了什么。如果有人可以提供他们的意见,我将不胜感激。
提前致谢
编辑: 我听从了 Sandeep107 的建议并将我的代码改回:
SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
我不再收到旧的错误代码,而是收到以下错误:
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
File "F:\Python33\lib\site-packages\oauth2client-1.4.7-py3.3.egg\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "F:\Python33\lib\site-packages\oauth2client-1.4.7-py3.3.egg\oauth2client\client.py", line 1487, in __init__
self.private_key = base64.b64encode(private_key)
File "F:\Python33\lib\base64.py", line 58, in b64encode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
再次感谢您的帮助。
检查link:http://gspread.readthedocs.org/en/latest/oauth2.html
我认为你需要 "client_email." 看起来你可能 "client_key" 两次。
告诉我你是怎么做出来的。我正在尝试自己解决整个 oauth2 问题,但进展并不顺利。
您应该按原样使用json_key['client_email']、json_key['private_key']。
无需替换为实际值
Python 3 要求将字符串转换为字节,Python 2.x 使用字符串,您使用的是哪个 Python 版本?