问题
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?
回答1:
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.
回答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.
回答3:
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?
回答4:
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
}
回答5:
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
回答6:
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.
来源:https://stackoverflow.com/questions/8702717/getting-first-name-last-name-email-from-twitter-using-oauth