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
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" }}}
you need to have the user_gender
scope now and have it approved by Facebook.
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",
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With