从facebook omniauth获取first_name和last_name字段 [英] get first_name and last_name fields from facebook omniauth
问题描述
我现在正在将omniauth功能实现到我的应用程序中。一切都很好,除了我不能从Facebook上得到第一个和最后一个名字。这是我的模型代码。
I am now implementing omniauth feature into my app. Everything works fine except that i cant get the first and last name from the facebook. Here is my model code.
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
我已经为devise正确设置了强大的参数,因为我现在使用的是默认身份验证,正在工作在Facebook上有任何额外的权限必要吗?
I already have the strong parameters setup properly for devise as am using that now for default authentication and is working properly.Is there any additional permissions necessary for first and last name from facebook?
推荐答案
在我找到解决方案之后,现在我想我们必须明确地要求我们需要的字段。对我而言,修复只是将 first_name
和 last_name
添加到 facebook
。
After some fiddling around i found the solution. Now i think we have to explicitly require the fields we require. For me the fix is just to add first_name
and last_name
to facebook
.
在我的初始化器
中,我添加了 first_name
和 last_name
到信息字段
。
In my initializers
i added first_name
and last_name
to info fields
.
info_fields: 'email, first_name, last_name'
strong>
Update
我的完整配置文件将如下所示
My full config file will look like this now
config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], scope: 'email', info_fields: 'email, first_name, last_name'
这篇关于从facebook omniauth获取first_name和last_name字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!