Is there a way to get your email address after authenticating with Gmail using Oauth?
Asked Answered
C

2

5

In a Python web application, I am able to connect to Gmail using OAuth and IMAP. OAuth uses whichever Google account you're currently signed into simply and asks you to grant access to the web app. However, it doesn't provide an API to actually retrieve that account's email address.

The problem is, even though the user grants access with OAuth, to interface with Gmail, you still need to explicitly provide the email address in your IMAP connection URL:

https://mail.google.com/mail/b/[your-email]/imap/

Because of this, the web application has to ask the user for their email address and ask them to grant access from Gmail with OAuth. What's worse is the email address they enter may not match the email of the account they grant access with, causing the app to fail.

Is there a way to get your email address using OAuth so you can put it in that URL without asking the user for it? I tried looking at this answer but Google returns a Bad Request whenever I use both https://mail.google.com/ and https://www.googleapis.com/auth/userinfo#email as my scope.

Callimachus answered 7/8, 2011 at 4:47 Comment(0)
N
4

What you're after is the Google Contacts API. If you're authorizing via OAuth you're probably currently asking for permission to access the gmail scope:

https://mail.google.com/mail/feed/atom

You will also need to ask for permission for the contacts scope:

https://www.google.com/m8/feeds/

Once you have that, you can make a GET request similar to the following:

https://www.google.com/m8/feeds/contacts/default/full?max-results=1

This should return a bunch of xml, here are some relevant bits:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/&quot;CEcMRX0_eCp7ImA9WhdRF00.&quot;">
<id>[email protected]</id>
...
<author>
  <name>Joe Bloggs</name>
  <email>[email protected]</email>
</author>
...
</feed>

As you can see you can find the authorized users' email in a couple of places.

If you're using OAuth you might also want to have a look at the Google OAuth playground, I've found it very handy: http://googlecodesamples.com/oauth_playground/index.php. If you decide to use OAuth2 there is equivalent tool at https://code.google.com/oauthplayground

Nottage answered 7/8, 2011 at 7:37 Comment(2)
That worked perfectly. Also the playground is quite nice, thanks for including that!Callimachus
To add to this answer, I also tried using max-results=0 and that worked. You get less XML back that way and still have access to <id>.Callimachus
F
1

skorks answer works just fine but you should use the correct API. By adding additional scope of

https://www.googleapis.com/auth/userinfo.email

You do it "the right way"!

I wrote a complete article about this with example code: http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/ Code available here: https://code.google.com/p/google-api-oauth-demo/

Fellmonger answered 21/10, 2013 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.