gpt4 book ai didi

python - 使用 OAuth2 在 gspread 中使用 python 进行授权的问题

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:37 31 4
gpt4 key购买 nike

我对 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

再次非常感谢任何帮助。

最佳答案

您应该按原样使用json_key['client_email']、json_key['private_key']

无需替换为实际值。

关于python - 使用 OAuth2 在 gspread 中使用 python 进行授权的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29851164/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com