Omniauth Facebook not returning email and gender rails 4

安稳与你 提交于 2019-12-13 14:24:53

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/32859313/omniauth-facebook-not-returning-email-and-gender-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!