Getting Twitter First Name / Last Name / Email Using OAuth

I use omniauth solely to allow access to my site using facebook / google / twitter.

I keep first name, last name and email address. However, when I raise the twitter hash from oauth, I only get the alias, name, location, image, description and URLs in the auth hash.

Is there an area that I can pass in my initializer to get the email address of the user and break the name into the fields first_name, last_name?

+6
source share
6 answers

Twitter does not issue user emails, so you cannot get this information from the Twitter API. Instead, you should ask the user to enter their email address in their registration form.

As for splitting the name up, you will do this as soon as you have a hash 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 may be nil if they don't have a place in their name or they don't give a last name. This also does not take into account the fact that many people have several surnames in other cultures.

+8
source

Using the current Twitter API, possibly by receiving an email. You must fill out a form requesting this permission. The process is simple and quick, explained here .

Requesting users email address requires your application to be included in the white list of Twitter. To request access, please use this form.

Once you have enabled the whitelist, the "Request email address from users" checkbox will be available under your application permissions on apps.twitter.com. The URL fields of the privacy policy and terms of service will also be available in the settings required to access the email. If enabled, users will be informed through the oauth / authorize dialog so that your application can access their email address.

+2
source

It’s good that Twitter by design will not give you the email id. This is a deliberate design decision by the API team.

Here is the same flow for your reference

Is there a way to get the email id of a user after checking her Twitter id with OAuth?

+1
source

If you have integration with FB, Google and Twitter, you will need to have the first and last in your database (if your user interface requires it), my business). This is what I came up with, as in some countries there are people with more than two tokens for their names: (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 } 
0
source

OmniAuth gives you all the names combined on one line. But some people have more than two words of names, such as "John Clark Smith." You can consider them in three 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 this name. This question is similar to this.

0
source

to use php:

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

Remember that a name can have several last names and, perhaps, several first names, this applies to several last names, but not the first names.

0
source

Source: https://habr.com/ru/post/904919/


All Articles