Last active
December 5, 2015 00:59
-
-
Save srgrn/0c6e9d8cb36860eaa422 to your computer and use it in GitHub Desktop.
example of how to get user email from google apis
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
| from bottle import route, request, redirect, view | |
| from apiclient import discovery | |
| # from apiclient import errors | |
| from oauth2client import client | |
| import httplib2 | |
| import json | |
| import os | |
| CLIENT_ID = 'YOURCLIENTID.apps.googleusercontent.com' | |
| CLIENT_SECRET = 'YOURSERCERET' | |
| REDIRECT_URI = 'http://YOURREDIRECTURI' # as added in the google developer console | |
| OAUTH_SCOPE = ['email'] # shorthand scope | |
| flow = client.OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) | |
| flow.params['access_type'] = 'offline' # in order to get the refresh token | |
| @route('/') | |
| @route('/home') | |
| def home(): | |
| authorize_url = flow.step1_get_authorize_url() | |
| redirect(authorize_url) | |
| @route('/YOURURLREDIRECT', method='GET') | |
| def redirected(): | |
| code = request.query.get('code') | |
| credentials = flow.step2_exchange(code) | |
| http = httplib2.Http() | |
| http = credentials.authorize(http) | |
| user_service = discovery.build('plus', 'v1', http=http).people() | |
| try: | |
| user = user_service.get(userId='me').execute() | |
| email = user['emails'][0]['value'] | |
| except AttributeError: | |
| print "Had some error in getting email this is not that important" | |
| email = None | |
| # do something with email | |
| # return some value for the app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment