omn​​iauth-facebook cannnot获取电子邮件地址 [英] omniauth-facebook cannnot get email address

查看:130
本文介绍了omn​​iauth-facebook cannnot获取电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了新的Rails应用程序,并安装了Devise和omniauth-facebook宝石。



将我的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
}
}
}

.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)
设计(3.5.1)

配置/ initializers / devise.rb

  config.omniauth:facebook,'appId','appSeacret',范围:'email,public_profile'
/ pre>

app / controllers / omniauth_callbacks_controller.rb

  def all_provider 
user = User.from_omniauth(request.env ['omniauth.auth'])
如果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

app / models / user.rb

  def from_omniauth(auth)
其中(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

该文档表示


声明性字段要尝试提高移动网络的性能,
v2.4中的节点和边缘要求您明确请求您的GET请求所需的
字段。例如,GET
/v2.4/me/feed默认不再包含喜欢和评论,但
GET /v2.4/me/feed?fields=comments,likes将返回数据。有关更多
详细信息,请参阅有关如何请求特定字段的文档。


所以,需要在config / initializers /devise.rb

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




  • 范围:'email' 是默认的



不仅仅是范围,还有info_fields。


I created new Rails App and install Devise and omniauth-facebook gem.

And setting my Facebook App, as testing environment.

So, I logged in via facebook and signed up my new Rails app, but request.env did not contain email address.info.

this is returned 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"
        }
    }
}

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

How can I get email address from facebook via oauth? Please anyone help me.

Rails ver is 4.2.3 Ruby ver is 2.2.2p95

This is Gem Versions

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

config/initializers/devise.rb

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

app/controllers/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

app/models/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

Add at 2015/7/11

I retried same code with my old test facebook application, and COULD get full public_profile and email.

Are any restrictions added for new facebook app?? someone knows?

解决方案

I found the way to get email address myself.

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

This doc says that

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.

so, it need to write in config/initializers/devise.rb

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

  • scope: 'email' is default

not only scope, but also info_fields.

这篇关于omn​​iauth-facebook cannnot获取电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆