4

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?

Kuldeep Kapade
  • 1,095
  • 3
  • 12
  • 17
  • What is the exact error you are getting? – Drahkar Feb 04 '12 at 22:34
  • This is the error i see: { "error": { "errors": [ { "domain": "com.google.auth", "reason": "invalidAuthentication", "message": "invalid token", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "invalid token" } } – Kuldeep Kapade Feb 04 '12 at 22:45
  • And I see this error after I successfully validate with - https://www.googleapis.com/oauth2/v1/tokeninfo – Kuldeep Kapade Feb 04 '12 at 22:49
  • I don't think `GoogleOAuth2Mixin.access_token` is the correct way to get the access token. I haven't tested this, but you should be able to get the it on the user info that comes back from `get_authenticated_user`. – Cole Maclean Feb 05 '12 at 18:22

2 Answers2

14

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

Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42
user1297061
  • 1,531
  • 2
  • 13
  • 15
0

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.

Shri
  • 2,129
  • 2
  • 22
  • 32