Skip to content

Instantly share code, notes, and snippets.

@srgrn
Last active December 5, 2015 00:59
Show Gist options
  • Select an option

  • Save srgrn/0c6e9d8cb36860eaa422 to your computer and use it in GitHub Desktop.

Select an option

Save srgrn/0c6e9d8cb36860eaa422 to your computer and use it in GitHub Desktop.
example of how to get user email from google apis
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