Created
December 22, 2014 14:11
-
-
Save mikej165/6521d4edeb507f2d9d95 to your computer and use it in GitHub Desktop.
Google Oauth 2 for http://blog.miguelgrinberg.com/post/oauth-authentication-with-flask
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
class GoogleSignIn(OAuthSignIn): | |
def __init__(self): | |
super(GoogleSignIn, self).__init__('google') | |
self.service = OAuth2Service( | |
name='google', | |
client_id=self.consumer_id, | |
client_secret=self.consumer_secret, | |
authorize_url='https://accounts.google.com/o/oauth2/auth', | |
access_token_url='https://accounts.google.com/o/oauth2/token', | |
base_url='https://www.googleapis.com/plus/v1/people/' | |
) | |
def authorize(self): | |
return redirect(self.service.get_authorize_url( | |
scope='email', | |
response_type='code', | |
redirect_uri=self.get_callback_url()) | |
) | |
def callback(self): | |
if 'code' not in request.args: | |
return None, None, None | |
oauth_session = self.service.get_auth_session( | |
data={'code': request.args['code'], | |
'grant_type': 'authorization_code', | |
'redirect_uri': self.get_callback_url()}, | |
decoder=json.loads | |
) | |
me = json.loads(oauth_session.get('me').text) | |
me_email = None | |
for e in me['emails']: | |
if e['type'] == 'account': | |
me_email = e['value'] | |
return ( | |
me.get('id'), | |
me.get('displayName'), | |
me_email) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment