omn​​iauth-facebook不散列不包含所有数据

时间:2017-01-02 15:54:13

标签: facebook ruby-on-rails-5 omniauth-facebook

我正在尝试通过omniauth-facebook gem启动登录并创建用户,我正在获取哈希值,但它没有我需要的所有数据,并且我没有尝试的东西,设置如下:< / p>

我想要的只是first_name,last_name,email和bio

会话控制器

  get     '/auth/:provider/callback', to: 'sessions#create'

路线

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, Rails.application.secrets.facebook_app_id,
    Rails.application.secrets.facebook_app_secret, 
    :scope => 'email,public_profile', :info_fields =>
    'name,email,first_name,last_name,bio'
  provider :twitter,  Rails.application.secrets.twitter_app_id,
    Rails.application.secrets.twitter_app_secret
end

omniauth.rb

{
  "provider": "facebook",
  "uid": "1**************",
  "info": {
    "name": "Philip Davies",
    "image": "http://graph.facebook.com/1*************/picture?type=square"
  },
  "credentials": {
    "token": "************",
    "expires_at": 1488480906,
    "expires": true
  },
  "extra": {
    "raw_info": {
      "name": "Philip Davies",
      "id": "1***********"
    }
  }
}

返回的哈希:

Load

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并且无法解决问题,但最终通过Koala gem(https://github.com/arsduo/koala)找到了解决方法。我最终使用facebook-omniauth gem登录,保存了该身份验证令牌,然后使用Koala从用户的Facebook个人资料中获取特定信息。

我的user.rb看起来像这样:

def self.from_omniauth(auth)
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.password = Devise.friendly_token[0,20]
        user.oauth_token = auth.credentials.token
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)
        @user_info = user.facebook.get_object(:me, { fields: [:first_name, :last_name, :email]})
        user.first_name = @user_info['first_name']
        user.last_name = @user_info['last_name']
        user.email = @user_info['email']
     end
end

def facebook
    Koala::Facebook::API.new(oauth_token)
end

我知道这不是完美的解决方案,但它让我可以做我想做的事情,所以也许它对你也有用。如果您有任何问题,请告诉我。