Getting First Name/Last Name/Email from Twitter using OAuth
Asked Answered
O

6

6

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?

Oberstone answered 2/1, 2012 at 15:47 Comment(0)
L
8

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.

Lytle answered 2/1, 2012 at 16:10 Comment(5)
Thank you much. I'm not quite sure how to handle asking for the email address after the callback - I'm pretty new to rails and currently just have the session create action handle this in one swoop without user interaction. Probably another question for SO.Oberstone
Unfortunately, Twitter forces you to use one additional step to your sign up process by not giving out the email address. You'd have to redirect the user to a sign up form asking them for their email address or else use a different sign up service like Facebook which provides the email address if you want it to be seamless.Lytle
Yeah - currently I'm using Facebook and Google - I'll probably just end up dropping Twitter due to this.Oberstone
You could always add it later with a check for if social_params["provider"] == "twitter" and redirect to a sign up form if necessary.Lytle
Is this still valid ? I'm only able to get screenName, and not name information.Alexina
H
2

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.

Handbook answered 12/5, 2016 at 8:46 Comment(0)
T
1

Well Twitter by Design will not pass you use email id.This is a deliberate design decision by the API team.

Here is same thread for your refrence

Is there a way to get an user's email ID after verifying her Twitter identity using OAuth?

Twoup answered 2/1, 2012 at 16:9 Comment(1)
Thanks - I should have searched better :/Oberstone
M
0

If you have integration with FB , Google and Twitter than you will need to have a first and last in your DB (if your UI requires it > my case). This is what I came up with as some countries have people with more than 2 tokens for their names ex: (Marco De Franca Solis) or (Marco, De Franca Solis)

// + TEST CASES
var name1 = handleName("Marco De Franca Solis")
var name2 = handleName("first,last wer wer")
var name3 = handleName("aName")
var name4 = handleName("")
// - TEST CASES

handleName = function(input) {
    var n=input.indexOf(" ");
    n += input.indexOf(",");
    var result = {};
    if(n < 0){
        result.first = input
        result.last = ""
    }
    else{
        arr =  input.split(/[ ,]+/);
        result.first = arr[0]
        result.last = ""
        if(arr.length > 1)
        {
            arr[0] = ""
            result.last = arr.join(" ");

        }
    }

    return result
}
Microbicide answered 3/2, 2014 at 0:35 Comment(0)
W
0

OmniAuth is giving you all the names combined in one string. But, some people have more than two-word names, such as "John Clark Smith". You can choose to treat those in three different ways:

(1) first_name: "John", last_name: "Smith"

  def first_name
    if name.split.count > 1
      name.split.first
    else
      name
    end
  end

  def last_name
    if name.split.count > 1
      name.split.last
    end
  end

(2) first_name: "John Clark", last_name: "Smith"

  def first_name
    if name.split.count > 1
      name.split[0..-2].join(' ')
    else
      name
    end
  end

  def last_name
    if name.split.count > 1
      name.split.last
    end
  end

(3) first_name: "John", last_name: "Clark Smith"

  def first_name
    name.split.first
  end

  def last_name
    if name.split.count > 1
      name.split[1..-1].join(' ')
    end
  end

The above examples assume that if the name contains less than 2 words then it is a first name. This question is similar to this one

Whorton answered 3/6, 2014 at 5:17 Comment(0)
C
0

for php use:

$names = explode(' ',$socialData->firstName);
$socialData->firstName=array_shift($names);
$socialData->lastName=implode(' ',$names);

Be aware the name could have multiple surnames and possibly multiple firstnames, this deals with multiple surnames but not firstnames.

Canadian answered 14/10, 2015 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.