gpt4 book ai didi

python - 使用 gdata 的托管域的 google 数据 API 的 OAuth 身份验证失败

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:18 25 4
gpt4 key购买 nike

我正在创建一个 django 应用程序,它为用户创建一个日历和一个 google 文档文件夹,并使用 API 来插入事件和添加文档。几个月前,它运行良好;现在我正在对我的代码进行重大重构,在测试上述组件时,我发现它们不再工作了!当我尝试创建文件夹或日历时,我从 API 得到了这样的响应:

RequestError: {'status': 401, 'body': '<HTML>\n<HEAD>\n<TITLE>Unknown authorization header</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Unknown authorization header</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n', 'reason': 'Unknown authorization header'}

我的代码看起来像这样 - 基于 this example在谷歌文档中-(文档代码较短,所以我将粘贴那个代码,尽管身份验证位实际上是两者共有的功能):

from gdata.auth import OAuthSignatureMethod, OAuthToken, OAuthInputParams
from gdata.calendar.service import CalendarService
from django.conf import settings
client_instance = CalendarService()
client_instance.SetOAuthInputParameters(OAuthSignatureMethod.HMAC_SHA1,
settings.OAUTH_CONSUMER_KEY,
consumer_secret=settings.OAUTH_CONSUMER_SECRET)
user_tokens = UserToken.objects.get(user=user)
if not user_tokens.oauth_access_token_value or not user_tokens.oauth_valid_token:
raise Exception('The user has not allowed us to access his services')
oauth_token=OAuthToken(key=user_tokens.oauth_access_token_value, secret=user_tokens.oauth_access_token_secret)
oauth_token.oauth_input_params = OAuthInputParams(OAuthSignatureMethod.HMAC_SHA1,
settings.OAUTH_CONSUMER_KEY,
consumer_secret=settings.OAUTH_CONSUMER_SECRET)
client_instance.SetOAuthToken(oauth_token)
new_folder = docs_service.CreateFolder("some folder")

异常来自最后一行,是 API 发生了变化还是只是我的问题? (我敢打赌这只是我,但我看不到它,因为它在几个月前就起作用了)

最佳答案

原因在 json 响应中列出:'reason': 'Unknown authorization header'

一个可能的原因是您机器的本地时间。确保本地机器上的时间是正确的,因为 oauth 使用当前的 unix 时间戳来签署和验证请求。 Google support forum

这是记录的错误列表。第一个似乎很适合您的情况:

希望这有帮助:)

关于python - 使用 gdata 的托管域的 google 数据 API 的 OAuth 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3170329/

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