【问题标题】:How to pass 'Authorization' header value in OAuth 2.0 with Google APIs如何使用 Google API 在 OAuth 2.0 中传递“授权”标头值
【发布时间】:2012-02-26 18:11:12
【问题描述】:

在这两种情况下,我都尝试使用 OAuth 1.0 和 2.0 访问 Google 的 API,我需要使用值“OAuth”填充标题中的授权字段,然后是访问令牌。我尝试了以下方法,但 Google 向我抛出了一个错误,提示 Authorization 标头值存在问题。我正在使用 Python-Tornado

additional_headers = {
        "Authorization": "OAuth "+GoogleOAuth2Mixin.access_token,
        "Accept-Encoding": None
    }
    h = httputil.HTTPHeaders()
    h.parse_line("Authorization: OAuth "+GoogleOAuth2Mixin.access_token)
    request = httpclient.HTTPRequest(self._USER_INFO_URL+"?access_token="+GoogleOAuth2Mixin.access_token, method="GET", headers=h)
    self.httpclient_instance.fetch(
        request,
        self.async_callback(callback)
    )

我尝试通过传递标头“h”和“additional_headers”来使用这两种方法,但它不起作用。什么是准确的方法?

【问题讨论】:

  • 你得到的确切错误是什么?
  • 这是我看到的错误:{ "error": { "errors": [ { "domain": "com.google.auth", "reason": "invalidAuthentication", "message" : "invalid token", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "invalid token" } }
  • 我在成功验证后看到了这个错误 - googleapis.com/oauth2/v1/tokeninfo
  • 我不认为GoogleOAuth2Mixin.access_token 是获取访问令牌的正确方法。我还没有对此进行测试,但您应该能够从 get_authenticated_user 返回的用户信息中获取它。

标签: python oauth-2.0 tornado


【解决方案1】:

那是因为它使用帐户电子邮件地址作为 UID,并且在身份验证流程中默认调用 userinfo 服务,因此您需要在范围列表中包含“userinfo.email”,否则身份验证流程将引发异常并失败返回令牌。

如果您使用OAuth 2.0 playground,请务必检查左侧栏中选择和授权 API 下的“用户信息-电子邮件”以及您要使用的 API。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我有同样的问题。如果将“ Bearer ”作为前缀包含在内,它就可以工作。

    Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多