Created
May 11, 2012 21:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?