Abh*_*ash 7 facebook ruby-on-rails 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_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
Run Code Online (Sandbox Code Playgroud)
我已经为设计正确设置了强大的参数,因为我现在使用它进行默认身份验证并且工作正常.是否还需要来自facebook的名字和姓氏的其他权限?
Abh*_*ash 23
经过一些摆弄我找到了解决方案.现在我认为我们必须明确要求我们需要的字段.对我来说,解决办法是只添加first_name
和last_name
到facebook
.
在我的initializers
我增加first_name
和last_name
到info fields
.
info_fields: 'email, first_name, last_name'
Run Code Online (Sandbox Code Playgroud)
更新
我的完整配置文件现在看起来像这样
config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], scope: 'email', info_fields: 'email, first_name, last_name'
Run Code Online (Sandbox Code Playgroud)