Rails 4 Facebook 身份验证 - 附加信息

Rails 4 Facebook authentication - additional information

我已经使用Whosebug 一年多了,总是通过搜索找到答案。但是,我找不到这个问题的答案,所以这周是我的第一个问题:

我跟随 Ryan Bates RailsCast #360 了解如何使用 omniauth-facebook gem 对 Rails 中的用户进行身份验证,并且在搜索了很长时间后设法找到了适应 Rails 4.

不幸的是,当我试图通过拉动 first_name 和 last_name 来扩展他的程序时,我无法得到它工作。我设法在另一个程序中获得了电子邮件地址,甚至图像,但 first_name 和 last_name 没有被发送。当我在 sessions#create

raise request.env["omniauth.auth"].to_yaml 时,我得到以下返回
--- !ruby/hash:OmniAuth::AuthHash 
provider: facebook 
uid: '105531523151136' 
info: !ruby/hash:OmniAuth::AuthHash::InfoHash 
name: Donna Alajhbhfedabi Bushakwitz 
image: http://graph.facebook.com/105531523151136/picture
...

这是我的 omniauth.rb 初始化程序

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret']
end

还有我的 user.rb 文件:

class User < ActiveRecord::Base
  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.first_name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end

任何帮助都会很棒。在过去的 3 天里,我一直在努力找出问题所在。

只需将此行添加到您的初始化程序中:

info_fields: 'first_name, last_name'