Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting First Name/Last Name/Email from Twitter using OAuth

I'm using omniauth exclusively to allow login to my website with facebook/google/twitter.

I store first name, last name, and email. However, when I raise the twitter auth hash from oauth I only get nickname, name, location, image, description and urls in the auth hash.

Is there a scope I can pass in my initializer to get the user's email and break name out into the first_name, last_name fields?

like image 979
Rapture Avatar asked Jan 02 '12 15:01

Rapture


2 Answers

Twitter does not give out user emails so you will not be able to get that information from the Twitter API. Instead, you have to ask the user to type in their email address on your sign up form.

As far as splitting the name up, you'd do that once you have the hash returned using something like:

social_params ||= request.env["omniauth.auth"]
fullname = social_params["user_info"]["name"].split(' ')
first_name, last_name = fullname[0], fullname[1]
puts "first name is #{first_name} and last name is #{last_name}"

Just keep in mind that last_name could be nil if they don't have a space in their name or they didn't give a last name. This also doesn't consider the fact that many people have multiple last names in other cultures.

like image 155
iwasrobbed Avatar answered Oct 31 '22 12:10

iwasrobbed


Using the current Twitter API is possible getting the email. You have to fill a form requesting that permission. The process is easy and quick, it is explained here.

Requesting a user’s email address requires your application to be whitelisted by Twitter. To request access, please use this form.

Once whitelisted, the “Request email addresses from users” checkbox will be available under your app permissions on apps.twitter.com. Privacy Policy URL and Terms of Service URL fields will also be available under settings which are required for email access. If enabled, users will be informed via the oauth/authorize dialog that your app can access their email address.

like image 23
Iván Rodríguez Torres Avatar answered Oct 31 '22 11:10

Iván Rodríguez Torres