Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omniauth Facebook not returning email and gender rails 4

I need to fetch name, email, image and gender from facebook. I am getting name and image, but email and gender is not fetched from facebook. I am struggling for past 2 days, can anyone help me out here.

User model:

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|       
    user.provider = auth.provider
    user.uid = auth.uid
    user.name = auth.info.name
    user.email = auth.info.email
    user.gender = auth.extra.raw_info.gender #if user.gender.blank?
    user.image = auth.info.image        
    user.save!
  end
end

Omniauth.rb

OmniAuth.config.logger = Rails.logger    
Rails.application.config.middleware.use OmniAuth::Builder do

if Rails.env.production?
  provider :facebook, 'APP_ID', 'APP_SEC'
elsif Rails.env.development?
  provider :facebook, 'APP_ID', 'APP_SEC', {:scope => 'publish_actions,email', :client_options => { :ssl => { :ca_file => "#{Rails.root}/config/ca-bundle.crt" }}}
else  
  provider :facebook, 'APP_ID', 'APP_SEC'
end  
end

If I use user.gender = auth.extra.raw.gender if user.gender.blank? it returns a null.

Even I have checked with my facebook privacy settings it is in public profiles only. Can anyone please help me out here

like image 541
saravana Avatar asked Dec 24 '22 13:12

saravana


2 Answers

Add these changes to get the data:

provider :facebook, 'APP_ID', 'APP_SEC_KEY', {:scope => 'email', :info_fields => 'email,name,first_name,last_name,gender', :client_options => { :ssl => { :ca_file => "#{Rails.root}/config/ca-bundle.crt" }}}
like image 51
Prashant4224 Avatar answered Jan 16 '23 02:01

Prashant4224


you need to have the user_gender scope now and have it approved by Facebook.

facebook developers interface

Then you can do something like:

    provider(
      :facebook,
      ENV.fetch("FACEBOOK_APP_ID"),
      ENV.fetch("FACEBOOK_APP_SECRET"),
      image_size: :large,
      secure_image_url: true,
      scope: "public_profile,email,user_gender",
      info_fields: "email,name,first_name,last_name,gender",
    )
like image 23
Dorian Avatar answered Jan 16 '23 00:01

Dorian