I am trying to get a new access token by using a refresh token as described by Google here. Google says I need to make a HTTP request. I had no clue how to do that, so I looked up how to do it from here. However, I must be doing the post incorrectly because I get an invalid_request
error.
Below is my relevant code:
h = Http()
post_data = {'POST': '/o/oauth2/token HTTP/1.1',
'HOST:': 'accounts.google.com',
'Content-Type:': 'application/x-www-form-urlencoded',
'client_id':ClientID,
'client_secret':ClientSecret,
'refresh_token':SavedRefreshToken,
'grant_type':'refresh_token'}
resp, content = h.request("https://accounts.google.com/o/oauth2/token",
"POST",
urlencode(post_data))
And the response I get is:
{
"error" : "invalid_request"
}
What am I missing here?
It's really just that you are sending 'Content-type'
in the body when it should be sent in headers. Additionally, you don't need 'POST': '/o/oauth2/token HTTP/1.1'
and 'HOST:': 'accounts.google.com'
in your body. Try doing this:
h = Http()
post_data = {'client_id':ClientID,
'client_secret':ClientSecret,
'refresh_token':SavedRefreshToken,
'grant_type':'refresh_token'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
resp, content = h.request("https://accounts.google.com/o/oauth2/token",
"POST",
urlencode(post_data),
headers=headers)
print content
It should print something like:
{
"access_token" : "ya29.AHBS6ZCtS8mBc_vEC9FFBkW2x3ipa7FLOs-Hi-3UhVkpacOm",
"token_type" : "Bearer",
"expires_in" : 3600
}