Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从facebook omniauth获取名字和姓氏字段_Ruby On Rails_Facebook_Devise_Omniauth_Omniauth Facebook - Fatal编程技术网

Ruby on rails 从facebook omniauth获取名字和姓氏字段

Ruby on rails 从facebook omniauth获取名字和姓氏字段,ruby-on-rails,facebook,devise,omniauth,omniauth-facebook,Ruby On Rails,Facebook,Devise,Omniauth,Omniauth Facebook,我现在正在我的应用程序中实现omniauth功能。一切都很好,只是我不能从facebook上得到名字和姓氏。这是我的模型代码 def self.from_omniauth(auth) user = User.where(email: auth.info.email).first if user return user else where(provider: auth.provider, uid: auth.uid).first_or_cr

我现在正在我的应用程序中实现omniauth功能。一切都很好,只是我不能从facebook上得到名字和姓氏。这是我的模型代码

    def self.from_omniauth(auth)
    user = User.where(email: auth.info.email).first
    if user
      return user
    else
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.first_name = auth.info.first_name
        user.last_name = auth.info.last_name
        user.email = auth.info.email
        user.image = auth.info.image
        user.password = Devise.friendly_token[0,20]
      end
    end
我已经为Desive正确设置了强参数,因为我现在使用它进行默认身份验证,并且工作正常。facebook上的名字和姓氏是否需要其他权限?

检查,
first\u name
last\u name
字段都是公共配置文件的一部分,因此您的权限应该可以

不知道这是否可行(事实上,我希望不会),但在实现中,我们正在生产中使用哈希访问器,而不是方法。因此:

new(
  first_name: oauth_data["info"]["first_name"],                  
  last_name:  oauth_data["info"]["last_name"],     
  ...
考虑到您的
电子邮件
等字段设置正确,如果尝试这样做会让我感到惊讶,但这可能值得一试


如果失败了,在创建可能会产生干扰的回调之前,您是否得到过任何验证或

在我找到解决方案之后。现在我认为我们必须明确地要求我们需要的字段。对我来说,修复方法就是将
名字和
姓氏添加到
facebook

在我的
初始值设定项中
我将
名字
姓氏
添加到
信息字段

info_fields: 'email, first_name, last_name'
更新

我的完整配置文件现在如下所示

   config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], scope: 'email', info_fields: 'email, first_name, last_name'

在为使用Desive+omniauth facebook的应用程序设计OAuth流时遇到此问题

使用public_profile作用域,可以在auth散列上返回name属性。
request.env[“omniauth.auth”]

我的问题是,用户有一个配置文件,并且该配置文件是自动保存的(用户需要在注册时输入名字和姓氏,并在配置文件模型上进行验证)

解决方案:在omniauth服务模型中创建名称解析方法,您可以将
request.env[“omniauth.auth”][“info”][“name”]
作为参数传递

# auth hash returns a name like "John Doe Smith"
def parse_name_from_string(ful_name_string) 
  
   name_array = name_string.split(" ")  # break apart the name

   # I chose to return a PORO so i can do something like parsed_user_name_from_facebook_auth.first_name
   {
     last_name: name_array.pop,
     first_name: name_array.join(" ")
   }
end

不,它也不起作用。我尝试了用户名而不是名字,而且效果很好。我对firstname和lastname进行了验证,但为了让ominiauth正常工作,我删除了验证。。他们有没有改变或者什么,或者名字和姓氏在信息里?很有趣!你用的是什么宝石?我们正在使用,我们不必这样做。不知道为什么…我正在使用gem“omniauth facebook”奇怪,很高兴你解决了它:)。你在哪里找到对
信息字段的引用的?
?从这里得到的。。。查看最后一条评论..一个救生员..谢谢Abhilash--但这是一个更新问题。以前的开发人员将应用程序固定在石器时代的gem版本上。