How to get user email after OAUTH with Google API Python Client
Solution 1
So I found a great way to do it!
Request the extra scope of https://www.googleapis.com/auth/userinfo.email then I can access that with a Gdata.Client to get the e-mail address.
Complete example code: https://code.google.com/p/google-api-oauth-demo/
Complete write up of how I got there: http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
Solution 2
I just want to add a resource I found to be particularly easier to use. Here it is: link.
Kallsbo directed me to a correct location by searching for scope https://www.googleapis.com/auth/userinfo.email
. After you already have credentials
, simply use the following function taken directly from that link:
def get_user_info(credentials):
"""Send a request to the UserInfo API to retrieve the user's information.
Args:
credentials: oauth2client.client.OAuth2Credentials instance to authorize the
request.
Returns:
User information as a dict.
"""
user_info_service = build(
serviceName='oauth2', version='v2',
http=credentials.authorize(httplib2.Http()))
user_info = None
try:
user_info = user_info_service.userinfo().get().execute()
except errors.HttpError, e:
logging.error('An error occurred: %s', e)
if user_info and user_info.get('id'):
return user_info
else:
raise NoUserIdException()
Call it user_email = get_user_info(credentials)['email']
, and you already have your email there! :)
Kristofer Källsbo
.Net, Python, Google App Engine, JavaScript, jQuery, Angular and a lot more. Into Raspberry Pi and other tinkering as well. Blogging @ hackviking.com and codeproject.com
Updated on June 16, 2022Comments
-
Kristofer Källsbo almost 2 years
I'm currently building a web app interacting with Google API in Python. Using the oauth to get access to the users resources. After successful authentication and upgrade of the token like this:
gd_client = gdata.photos.service.PhotosService() gd_client.SetAuthSubToken(token) gd_client.UpgradeToSessionToken()
Then I can access the different feeds of the API and get a list of the users Youtube videos for example. But the user has only logged in with Google and all I have is a oauth token and no other info on the user. How do I retrieve info on the user? Like email, display name and so on? I have been testing a lot of different stuff without managing to solve this...
I found some interesting here: Is there a way to get your email address after authenticating with Gmail using Oauth?
My theory was that I could use the PhotoService.GetAuthSubToken() and then reuse that token to request the contact and get auther.email from the contact entry. Changed the scope of the auth to:
scope = ['https://picasaweb.google.com/data/', 'https://www.google.com/m8/feeds/']
witch returns a roken valid for both services... Any ideas?
-
marty331 over 10 yearsKristofer,I've been looking for this for days! thanks so much for posting the blog!!
-
Kristofer Källsbo over 10 yearsmarty331: Happy to share it, it took me a while and a lot of reading, testing and pepsi before I cracked it!
-
swdev over 9 yearsMan you just save my day! Thanks!
-
MoorzTech almost 8 yearsYES! Thanks alot! This is the way to do it with the official google-api library.