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

发布于 2024-09-08 16:45:31 字数 1845 浏览 4 评论 0原文

我正在创建一个 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'}

我的代码看起来像这样 - 基于 google 文档中的这个示例 - (文档代码较短,所以我将粘贴该代码,尽管身份验证位实际上是两者共有的函数):

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 中是否发生了某些更改,或者只是我? (我敢打赌这只是我,但我看不到它,因为它在几个月前起作用)

I'm creating a django app that creates a calendar and a google docs folder for the users, and uses the API to insert events and add documents. A few months ago, it worked nice enough; now I'm doing a major refactoring of my code and, while testing the aforementioned components, I discovered that they don't work anymore! When I try to create a folder or a calendar, i get this response from the 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'}

My code looks like this -based on this example in the google documentation- (the docs code is shorter, so I'll paste that one, though the authentication bit is actually a function common to both):

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")

The exception comes from that last line, has something changed in the API or it's just me? (I bet it's just me, but I can't see it since it worked a couple of months ago)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-09-15 16:45:31

原因在 json 响应中列出: 'reason': '未知授权标头'

一个可能的原因是您计算机的本地时间。确保本地计算机上的时间正确,因为 oauth 使用当前的 unix 时间戳来签名和验证请求。 Google 支持论坛

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

希望这会有所帮助 :)

The reason is layed out in the json response: 'reason': 'Unknown authorization header'

One possible cause is your machine's local time. Make sure that the time on your local machine is correct, as oauth uses the current unix timestamp to sign and verify requests. Google support forum

Here's a list of bugs logged. The first one seems to fit your case well:

Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文