Facebook authentication data returns nil, except name and email
Asked Answered
K

3

6

I used the Devise gem to set up a User model for my app. I'm trying to work in Facebook authentication using Omniauth. I can retrieve the Name and Email data, but I'm having trouble getting any other public_profile data. In this example I'm trying to get Gender, but none of the other data works either. When I visit this path: user_omniauth_authorize_path(:facebook), the "facebook" action in "controllers/registrations_controller.rb" is called. But for the user that is created, user.gender, and all other data other than name and email, comes back nil.

config/initializers/devise.rb

config.omniauth :facebook, "<ID>", "<SECRET>", scope: 'email', display: 'popup', info_fields: 'email,name,gender'

app/models/devise.rb

  devise :omniauthable, :omniauth_providers => [:facebook]
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.gender = auth.extra.raw_info.gender.to_s
    end
  end

app/controllers/registrations_controller.rb

  def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

gemfile

gem 'devise'
gem 'omniauth-facebook'

What's strange is I can't even get the class type of the data. Name and Email return "String" if I call class.to_s on them, but all the other data returns "NilClass".

What am I doing wrong?

UPDATE:

Okay I literally have no idea what could have possibly changed, but this same exact code now suddenly works....so problem solved I guess?

Kepler answered 9/9, 2016 at 3:31 Comment(6)
Do you mean null?Xenon
Friendly reminder: facebook will not provide you anything except Name and Email unless your app becomes whitelisted. In fact, you have to justify that your app will be inconsistent without additional information about a facebook user that you are trying to receive from their API.Xenon
@RustamUmarov Wait, really? I never saw that in any of the documentation. How do I get my app whitelisted?Kepler
As I said you have to submit it for a review by their team. It is done through the app dashboard. Anything except public info(first name, last name and email) has to be approved firstly.Xenon
@RustamUmarov Are you sure though? All the data I'm trying to get is included in the "public profile" information. According to this, it should be included as default information: developers.facebook.com/docs/facebook-login/…Kepler
I was pretty sure since I experienced this on my own 2 months ago. In your case should not have any problems. You will be asked for a permission in case you are trying to retrieve advanced values like user_education_history, user_events etc. Seems like they modified the documentation. Sorry for providing wrong information.Xenon
C
1

You can gender attribute using auth.info.gender

where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  user.gender = auth.info.gender
end
Carnatic answered 9/9, 2016 at 7:7 Comment(1)
That returns "nil" as well. I'm pretty sure gender is only accessible through auth.extra.raw_info based on the documentation here: github.com/mkdynamic/omniauth-facebook#auth-hash but auth.extra.raw_info.gender also returns "nil".Kepler
W
1

You sure the user has these data as public info?

I would try to debug the permissions in Facebook's open graph explorer https://developers.facebook.com/tools/explorer/

That will help you understand wether it is permissions, lacking data issue or a problem with the integration.

Whooper answered 14/9, 2016 at 19:55 Comment(2)
That data is listed under public_profile, so shouldn't it be publicly available by default?Kepler
check it out here: developers.facebook.com/docs/facebook-login/… be sure you declare it and make sure the user you are trying to test has indeed the extra properties.Whooper
W
1

Try This way

def self.from_omniauth(access_token)
  data = access_token.info
  email      = data.info.email
  first_name = data.first_name
  last_name  = data.last_name
end
Whiting answered 17/9, 2016 at 4:43 Comment(2)
I'm still getting the same result.Kepler
you can do one thing some times it will not return all data it will happen very rare so you can use FB Graph Api to get other detailsWhiting

© 2022 - 2024 — McMap. All rights reserved.