【问题标题】:Rails 4 Facebook authentication - additional informationRails 4 Facebook 身份验证 - 附加信息
【发布时间】:2015-12-02 21:26:04
【问题描述】:

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

我关注了 Ryan Bates RailsCast #360,了解如何使用 omniauth-facebook gem 在 Rails 中验证用户,并在搜索了很长时间后设法将其适应 Rails 4。

不幸的是,当我尝试通过拉取名字和姓氏来扩展他的程序时(意味着?)与散列中的其他详细信息一起发送,我无法让它工作。我设法在另一个程序甚至图像中获取了电子邮件地址,但没有发送 first_name 和 last_name。当我在 session#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 天里,我一直在试图找出问题所在。

【问题讨论】:

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


    【解决方案1】:

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

    info_fields: 'first_name, last_name'
    

    【讨论】:

    • 谢谢!这有效,但我还必须重新启动服务器。我想我昨天试过了,但没有重新启动服务器(并且我的代码中可能还有其他一些错误)。但再次感谢!
    猜你喜欢
    • 2015-03-16
    • 2016-02-26
    • 2018-10-30
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多