Get fields first_name and last_name from facebook omniauth

Now I use the omniauth function in my application. Everything works fine, except that I cannot get the first and last name from facebook. Here is my model code.

def self.from_omniauth(auth) user = User.where(email: auth.info.email).first if user return user else where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.provider = auth.provider user.uid = auth.uid user.first_name = auth.info.first_name user.last_name = auth.info.last_name user.email = auth.info.email user.image = auth.info.image user.password = Devise.friendly_token[0,20] end end 

I already have the right settings for the development, as I use it now for default authentication and it works correctly. Are there any additional permissions required for the first and last name from facebook?

+7
ruby-on-rails facebook devise omniauth omniauth-facebook
source share
2 answers

After some reversal, I found a solution. Now I think that we should explicitly require the required fields. For me to fix just add first_name and last_name to facebook .

In my initializers I added first_name and last_name to info fields .

 info_fields: 'email, first_name, last_name' 

Update

Now my complete configuration file will look like this

  config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], scope: 'email', info_fields: 'email, first_name, last_name' 
+23
source share

From checking documents on Facebook, the first_name and last_name fields are part of the general profile, so your permissions should be in order.

I don’t know if this will work (in fact, I would hope that this is not so), but in the implementation we are working in production at the moment when we use Hash accessors instead of methods. So:

 new( first_name: oauth_data["info"]["first_name"], last_name: oauth_data["info"]["last_name"], ... 

Given that your fields are email , etc. installed correctly, I would be surprised if I tried to do it, but it might be worth it.

Otherwise, do you have any checks or before_create callbacks that might intervene somehow?

+1
source share

All Articles