How does one retrieve the email address of a user

2020-02-12 13:27发布

问题:

I'm trying to use GData to retrieve the email address, real name, and profile URL of the user that just authorized my site using Google OAuth.

We know how to request it using Google's OpenID flow, but the OpenID flow has the severe limitation that we have to ask for a Google Apps user's domain before we know where to send them to log in. At least using OAuth (or even AuthSub), the user gets prompted for which of their Google accounts to log in.

回答1:

Still not clear that this is possible, but we now work around it by using the OpenID flow. With the addition of their universal login flow, we no longer have reason to avoid their OpenID flow.



回答2:

If you fetch the user's contacts feed, you can access the authors field which gives you email and name. Additionally, the feed's id field appears to be the email address of the person who owns the contacts.

An example (in Scala) with names changed, assuming the user of AuthSub (sorry, I've not migrated my code to OAuth) where you already have a session token:

scala> val contacts_service = new ContactsService("foo")
contacts_service: com.google.gdata.client.contacts.ContactsService = com.google.gdata.client.contacts.ContactsService@3fd1acee

scala> contacts_service.setAuthSubToken(token, null)

scala> val feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full?max-results=10000")
feedUrl: java.net.URL = https://www.google.com/m8/feeds/contacts/default/full?max-results=10000

scala> val feed = contacts_service.getFeed(feedUrl, classOf[ContactFeed])
feed: com.google.gdata.data.contacts.ContactFeed = {ContactFeed com.google.gdata.data.contacts.ContactFeed@271a95f8}

scala> feed.getId
res13: java.lang.String = user@example.com

scala> val p = feed.getAuthors.head
p: com.google.gdata.data.Person = com.google.gdata.data.Person@513b4686

scala> p.getEmail
res14: java.lang.String = user@example.com

scala> p.getName
res15: java.lang.String = Example User