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?

Joe Morano
  • 1,715
  • 10
  • 50
  • 114
  • Do you mean null? – Coke Sep 13 '16 at 06:27
  • 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. – Coke Sep 13 '16 at 06:31
  • @RustamUmarov Wait, really? I never saw that in any of the documentation. How do I get my app whitelisted? – Joe Morano Sep 14 '16 at 07:11
  • 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. – Coke Sep 14 '16 at 07:23
  • @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: https://developers.facebook.com/docs/facebook-login/permissions#reference-public_profile – Joe Morano Sep 14 '16 at 21:53
  • 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. – Coke Sep 15 '16 at 02:24

3 Answers3

3

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
bk chovatiya
  • 343
  • 2
  • 8
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
Prashant4224
  • 1,551
  • 1
  • 14
  • 21
  • That returns "nil" as well. I'm pretty sure `gender` is only accessible through `auth.extra.raw_info` based on the documentation here: https://github.com/mkdynamic/omniauth-facebook#auth-hash but `auth.extra.raw_info.gender` also returns "nil". – Joe Morano Sep 09 '16 at 20:30
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.

Guy Dubrovski
  • 1,542
  • 1
  • 20
  • 25
  • That data is listed under public_profile, so shouldn't it be publicly available by default? – Joe Morano Sep 18 '16 at 03:19
  • check it out here: https://developers.facebook.com/docs/facebook-login/permissions#reference-public_profile be sure you declare it and make sure the user you are trying to test has indeed the extra properties. – Guy Dubrovski Sep 18 '16 at 13:12