Ruby on Rails Omniauth facebook 不回复电子邮件 [英] Ruby on Rails Omniauth facebook doesn't return email

查看:17
本文介绍了Ruby on Rails Omniauth facebook 不回复电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天来一直在尝试为 Facebook 设置我的 Omniauth,但我不知道我做错了什么.

I have been trying for days to setup my Omniauth for facebook I don't know what am I doing wrong.

我无法收到用户的电子邮件.返回的哈希只包含name"和uid",甚至不包含first_name"和last_name"

I am not able to get the email of the user. The returned hash only contains the "name" and the "uid" not even "first_name" and "last_name"

设计.rb:

  config.omniauth :facebook, "KEY", "SECRET"

omniauth_callbacks_controller.rb:

omniauth_callbacks_controller.rb:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    logger.info request.env["omniauth.auth"]
    @user = User.from_omniauth(request.env["omniauth.auth"])
    sign_in_and_redirect @user
  end
end 

user.rb:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable,
       :omniauthable, :omniauth_providers => [:facebook]

has_many :authentications

def self.from_omniauth(auth)
  logger.info auth
  user = where(email: auth.info.email).first
  if(user != nil)
    user.authentications.where(provider: auth.provider, uid: auth.uid).first_or_create do |l|
      user.authentications.create!(user_id: user.id,
                                  provider: auth.provider,
                                  uid: auth.uid)
    end
  else
    user = User.create!(email: auth.info.email,
                       password: Devise.friendly_token[0,20],
                       first_name: auth.info.first_name,
                       last_name: auth.info.last_name)
    user.authentications.create!(user_id: user.id,
                                provider: auth.provider,
                                uid: auth.uid)
  end
  user
end
end

registrations_controller.rb:

registrations_controller.rb:

class RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
  end
end

routes.rb:

  devise_for :users, :controllers => { registrations: 'registrations', omniauth_callbacks: 'omniauth_callbacks' }

返回的哈希值:

#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1444504014 token="TOKEN">
extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash id="1506781179612589" name="Ayman Salah">> info=#
<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/1506781179612589/picture" name="Ayman Salah"> provider="facebook" uid="1506781179612589">

推荐答案

我只需要通过在 devise.rb 中添加这个来检索电子邮件:

I only got to retrieve the email by adding this in devise.rb:

  config.omniauth :facebook, "KEY", "SECRET", scope: 'email', info_fields: 'email, name'

这篇关于Ruby on Rails Omniauth facebook 不回复电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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