Rails:在Facebook Oauth之后获取用户的电子邮件(使用Devise& facebook omniauth gem)

时间:2015-08-20 08:29:37

标签: ruby-on-rails facebook devise omniauth

我在从Facebook注册后访问用户的电子邮件,first_name和last_name时遇到问题。

这是我的Omniauth.rb的样子:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'],
  :scope => 'email',
  :info_fields=>'first_name, last_name, email'
end

当Facebook登录窗口显示时,它会正确显示范围("此应用程序可以访问您的电子邮件")。但是当我撬开回应时,我发现我没有得到任何我要求的字段。

   {"provider"=>"facebook",
 "uid"=>"10101161800900201",
 "info"=>
  {"name"=>"Jackson Cunningham",
   "image"=>"http://graph.facebook.com/10101161800900201/picture"},
 "credentials"=>
  {"token"=>
    "...",
   "expires_at"=>1445243073,
   "expires"=>true},
 "extra"=>
  {"raw_info"=>
    {"name"=>"Jackson Cunningham", "id"=>"10101161800900201"}}}

1 个答案:

答案 0 :(得分:4)

想出来。除了Omniauth.rb之外,您还需要确保编辑Devise.rb

Devise.rb应该有:

config.omniauth :facebook, "your_facebook_id", "your_facebook_secret", scope: 'email,public_profile', info_fields: 'email, first_name, last_name'

替换您需要的任何特定info_fields(所有可用的here列表)