为什么没有检索到facebook电子邮件?

时间:2012-11-17 04:20:45

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

在用户接受用他的Facebook帐户登录后,我的应用程序回调将由facebook调用,我注意到facebook没有检索到用户的电子邮件,虽然在omniauth的配置中,我列出了电子邮件这样的许可列表:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, omniauth_app_id, omniauth_app_secret_id,
           :scope => 'email,user_birthday,read_stream', :display => 'popup'
end

我希望在request.env['omniauth.auth']['extra']['raw_info']['email']request.env['omniauth.auth']['info']['raw_info']['email']中找到电子邮件 但是,它不在那里......事实上,它不在request对象中的任何属性中!

有什么想法吗?它与我的app facebook设置有关吗?

修改

这是来自Facebook的回电结果:

puts auth.inspect

#<OmniAuth::AuthHash credentials=#<Hashie::Mash expires=true expires_at=1353164400 token="***"> extra=#<Hashie::Mash raw_info=#<Hashie::Mash first_name="***" id="***" last_name="***" link="***" locale="ar_AR" name="***" timezone=2 updated_time="2012-11-17T13:01:59+0000" username="***" verified=true>> info=#<OmniAuth::AuthHash::InfoHash first_name="***" image="***" last_name="***" name="***" nickname="***" urls=#<Hashie::Mash Facebook="***"> verified=true> provider="facebook" uid="***">

我已使用 * 替换了reall数据,但是,您可以看到电子邮件数据丢失..

EDIT2

这是我在Gemfile中使用的宝石     宝石'设计'

gem 'omniauth'
gem 'omniauth-facebook', '1.4.0'

gem 'oauth2'

4 个答案:

答案 0 :(得分:4)

好的,我发现了!以下是facebook所说的here

  

默认情况下,调用FB.login将尝试对用户进行身份验证   只有基本权限。如果你想要一个或多个额外的   权限,使用选项对象调用FB.login,并设置范围   带有逗号分隔的权限列表的参数   来自用户的请求。

所以,我必须像这样添加'email':

 FB.login(function(response) {
   // handle the response
 }, {scope: 'email,user_likes'});

非常感谢所有试图提供帮助的人: - )

答案 1 :(得分:2)

可以在没有确认电子邮件的情况下拥有Facebook帐户。在这种情况下,request.env['omniauth.auth']['info']没有'email'密钥。

答案 2 :(得分:1)

如何从info哈希中获取它?

request.env['omniauth.auth']['info']['email']

我认为这是omniauth-facebook在Auth Hash部分中推荐的内容

答案 3 :(得分:1)

我们在facebook上使用devise和omniauth - 遵循以下railscast。我不知道你是否能适应,但我们已经成功地提取了电子邮件和其他一些信息。来自回调。

http://asciicasts.com/episodes/241-simple-omniauth

我们的控制器操作如下(浓缩):

  def create  
    omniauth = request.env["omniauth.auth"]  
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])  
    if authentication  
      sign_in_and_redirect(:user, authentication.user)  
    elsif current_user  
      current_user.authentications.create(:provider => omniauth ['provider'], :uid => omniauth['uid'])  
      redirect_to authentications_url  
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save
        sign_in_and_redirect(:user, user)
      else   
        session[:omniauth] = omniauth.except('extra')
        redirect_to new_user_registration_url
       end
    end  
  end

User.apply_omniauth:

   def apply_omniauth(omniauth)  
     authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid']) 
   end

根据其他人的建议,我们收到了这封电子邮件:

 @omniauth_email = session[:omniauth][:info][:email]

我希望这会有所帮助,我知道你所寻找的并不安静。

- 编辑 -

我的初始化程序与您的不同。尝试删除所有xs的东西,并使用这样的东西:

 Rails.application.config.middleware.use OmniAuth::Builder do  
  provider :facebook, 'xxxxxx', 'xxxxxx'  
 ...
 end