ruby-on-rails – omniauth-facebook cannnot获取电子邮件地址

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – omniauth-facebook cannnot获取电子邮件地址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了新的Rails应用程序,并安装了Devise和omniauth-facebook gem.

并设置我的Facebook应用程序,作为测试环境.

所以,我通过Facebook登录注册了我的新Rails应用程序,但request.env没有包含电子邮件地址.info.

这是返回request.env [‘omniauth.auth’]

{
   "provider" => "facebook","uid" => "xxxxxxxxxxxx","info" => {
         "name" => "xxxxxxx","image" => "http://graph.facebook.com/xxx/picture"
    },"credentials" => {
             "token" => "tokenstring","expires_at" => xxxxxxxxx,"expires" => true
    },"extra" => {
        "raw_info" => {
            "name" => "xxx xxxx","id" => "xxxxxxxxx"
        }
    }
}

它架子request.env [‘omniauth.auth’] [‘info’] [’email’]

如何通过oauth从Facebook获取电子邮件地址?请有人帮我

Rails ver为4.2.3
Ruby ver是2.2.2p95

这是宝石版本

omniauth (1.2.2)
 omniauth-facebook (2.0.1)
 devise (3.5.1)

配置/初始化/ devise.rb

config.omniauth :facebook,'appId','appSeacret',scope: 'email,public_profile'

应用程序/控制器/ omniauth_callbacks_controller.rb

def all_provider
    user = User.from_omniauth(request.env['omniauth.auth'])
    if user.persisted?
      sign_in_and_redirect user
    else
      session['devise.user_attributes'] = user.attributes
      redirect_to new_user_registration_url
    end
  end

  alias_method :facebook,:all_provider

应用程序/模型/ user.rb

def from_omniauth(auth)
  where(provider: auth.provider,uid: auth.uid).first_or_create do |user|
    user.email = auth.info.email
  end
end

在2015/7/11添加

我用旧的测试Facebook应用程序重试了相同的代码,并且可以获得完整的public_profile和电子邮件.

是否添加新的Facebook应用程序的任何限制?有人知道吗

解决方法

我找到了自己的电子邮件地址的方式.

https://developers.facebook.com/docs/apps/changelog#v2_4

这个文件

Declarative Fields To try to improve performance on mobile networks,
Nodes and Edges in v2.4 requires that you explicitly request the
field(s) you need for your GET requests. For example,GET
/v2.4/me/Feed no longer includes likes and comments by default,but
GET /v2.4/me/Feed?fields=comments,likes will return the data. For more
details see the docs on how to request specific fields.

所以,它需要在config / initializers / devise.rb中写入

config.omniauth :facebook,'app_id','app_secret',scope: 'email',info_fields: 'email'

范围:’email’是默认值

不仅仅是范围,还有info_fields.

猜你在找的Ruby相关文章