How to pass 'Authorization' header value in OAuth 2.0 with Google APIs

夙愿已清 提交于 2019-12-22 09:48:35

问题


I am trying to access Google's APIs with OAuth 1.0 and 2.0 in both cases I need to fill Authorization field in the headers with value 'OAuth' followed by access token. I tried following method, but Google throws me an error saying there is problem in Authorization header values. I am using 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)
    )

I tried using both methods, by passing header 'h' and 'additional_headers', but it doesn't work. What is an accurate method?


回答1:


I had same problem. It works if 'Bearer ' is included as prefix.

Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42



回答2:


Thats because it uses account email address as a UID and it calls the userinfo service by default during the authentication flow, so you need to include "userinfo.email" in your scopes list otherwise the authentication flow will raise and exception and fail to return the tokens.

If you are using OAuth 2.0 playground be sure to check "Userinfo-Email" under Select and authorize API's on left bar along with the API you want to use. Hope this helps.



来源:https://stackoverflow.com/questions/9140287/how-to-pass-authorization-header-value-in-oauth-2-0-with-google-apis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!