4

我需要从 facebook 获取姓名、电子邮件、图像和性别。我正在获取姓名和图片,但未从 Facebook 获取电子邮件和性别。我在过去的两天里一直在挣扎,有人可以在这里帮助我吗?

用户型号:

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

如果我使用user.gender = auth.extra.raw.gender if user.gender.blank?它返回一个空值。

即使我已经检查了我的 facebook 隐私设置,它也只在公共个人资料中。任何人都可以在这里帮助我吗

4

2 回答 2

9

添加这些更改以获取数据:

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" }}}
于 2015-09-30T07:20:30.857 回答
1

您现在需要拥有user_gender范围并获得 Facebook 的批准。

脸书开发者界面

然后您可以执行以下操作:

    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",
    )
于 2021-01-05T23:35:31.467 回答