How does one retrieve the email address of a user with GData?

余生长醉 提交于 2020-01-10 04:13:06

问题


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


来源:https://stackoverflow.com/questions/2484188/how-does-one-retrieve-the-email-address-of-a-user-with-gdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!