ruby-on-railsruby-on-rails-3facebook-graph-apiruby-on-rails-4omniauth-facebook

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


Solution

  • 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" }}}