加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:59:40 所属栏目:百科 来源:网络整理
导读:我创建了新的Rails应用程序,并安装了Devise和omniauth-facebook gem. 并设置我的Facebook应用程序,作为测试环境. 所以,我通过Facebook登录并注册了我的新Rails应用程序,但request.env没有包含电子邮件地址.info. 这是返回request.env [‘omniauth.auth’] {
我创建了新的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读