Facebook身份验证数据返回nil,但名称和电子邮件除外

Joe*_*ano 6 facebook ruby-on-rails devise omniauth omniauth-facebook

我使用Devise gem为我的应用程序设置用户模型.我正在尝试使用Omniauth进行Facebook身份验证.我可以检索NameEmail数据,但我无法获取任何其他public_profile数据.在这个例子中,我试图得到Gender,但其他数据都没有.当我访问此路径时:user_omniauth_authorize_path(:facebook),调用"controllers/registrations_controller.rb"中的"facebook"操作.但是对于创建的用户user.gender,以及除姓名和电子邮件之外的所有其他数据,都会返回零.

配置/初始化/ devise.rb

config.omniauth :facebook, "<ID>", "<SECRET>", scope: 'email', display: 'popup', info_fields: 'email,name,gender'
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ devise.rb

  devise :omniauthable, :omniauth_providers => [:facebook]
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.gender = auth.extra.raw_info.gender.to_s
    end
  end
Run Code Online (Sandbox Code Playgroud)

应用程序/控制器/ registrations_controller.rb

  def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
Run Code Online (Sandbox Code Playgroud)

的Gemfile

gem 'devise'
gem 'omniauth-facebook'
Run Code Online (Sandbox Code Playgroud)

奇怪的是我甚至无法获得数据的类类型.NameEmail返回"字符串"如果我叫class.to_s上他们,但所有其他数据恢复"NilClass".

我究竟做错了什么?

更新:

好吧,我真的不知道什么可能会改变,但现在这个相同的代码突然起作用....所以问题解决了我猜?

Pra*_*224 1

您可以gender使用属性auth.info.gender

where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  user.gender = auth.info.gender
end
Run Code Online (Sandbox Code Playgroud)