Skip to content

Instantly share code, notes, and snippets.

@roosmaa
Created May 11, 2012 21:49
Show Gist options
  • Save roosmaa/2662592 to your computer and use it in GitHub Desktop.
Save roosmaa/2662592 to your computer and use it in GitHub Desktop.
Using the refresh token to obtain the access token from Google OAuth 2.0 server
import logging
import urllib
import urllib2
import json
log = logging.getLogger(__name__)
def authenticate(refresh_token, client_id, client_secret):
"""Authenticates with the Google servers to get authentication token
for C2DM API calls.
Returns the authentication token, if successful.
"""
data = urllib.urlencode({
'grant_type': 'refresh_token',
'client_id': client_id,
'client_secret': client_secret,
'refresh_token': refresh_token,
})
req = urllib2.Request('https://accounts.google.com/o/oauth2/token', data)
try:
resp = urllib2.urlopen(req)
return json.loads(resp.read()).get('access_token', None)
except urllib2.HTTPError, e:
log.error('HTTP error with status code %d occured: %s',
e.code, e.read(), exc_info=True)
except:
log.error('Failed to authenticate with Google.', exc_info=True)
return None
@Batush0
Copy link

Batush0 commented Aug 11, 2023

okay but how do i supposed to acceess accurate refresh_token , if the way storing it into client ist secure and access_token already retired then how do i supposed to obtain it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment