Ruby Rails Omniauth facebook 没有 return 电子邮件
Ruby on Rails Omniauth facebook doesn't return email
几天来我一直在尝试为 Facebook 设置我的 Omniauth 我不知道我做错了什么。
我无法获取用户的电子邮件。返回的哈希仅包含 "name" 和 "uid" 甚至不包含 "first_name" 和 "last_name"
devise.rb:
config.omniauth :facebook, "KEY", "SECRET"
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:
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">
您需要申请权限。例如,请求 email
、user_birthday
和 read_stream
权限并在弹出窗口中显示身份验证页面 window:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
:scope => 'email,user_birthday,read_stream', :display => 'popup'
end
如果您有任何疑问,请检查此Omniauth-facebook。
我只能通过在 devise.rb 中添加这个来检索电子邮件:
config.omniauth :facebook, "KEY", "SECRET", scope: 'email', info_fields: 'email, name'
确保您没有在信息字段请求中放置空格(另一个答案可能打错了这个)。
您需要特别要求从信息字段哈希中返回电子邮件,因为默认情况下并非如此。
如果您在不使用 Devise 的情况下使用 Omniauth-Facebook,请在 config/initializers/omniauth.rb 文件中进行如下设置:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, Rails.application.secrets.omniauth_provider_key, Rails.application.secrets.omniauth_provider_secret,
:scope => 'email', :display => 'popup', :info_fields => 'name,email'
end
此信息有点隐藏在 omniauth-facebook gem 的 GitHub 自述文件 Configuring 部分的末尾。
使用 Devise plus Omniauth,但是,您可以像这样在 config/initializers/devise.rb 中设置它(字符串中没有空格!):
config.omniauth :facebook, 'app_id', 'app_secret', scope: 'email', info_fields: 'name,email'
我遇到了同样的问题。我让它可以更新 omniauth 和 omniauth-facebook gems。
我遇到了同样的问题,但事实证明这是一个不同的问题,如果您使用客户端流程,请确保在您的客户端上请求您需要的范围:
FB.login(function(response) {
// hit API callback endpoint
}, {scope: 'email'});
几天来我一直在尝试为 Facebook 设置我的 Omniauth 我不知道我做错了什么。
我无法获取用户的电子邮件。返回的哈希仅包含 "name" 和 "uid" 甚至不包含 "first_name" 和 "last_name"
devise.rb:
config.omniauth :facebook, "KEY", "SECRET"
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:
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">
您需要申请权限。例如,请求 email
、user_birthday
和 read_stream
权限并在弹出窗口中显示身份验证页面 window:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
:scope => 'email,user_birthday,read_stream', :display => 'popup'
end
如果您有任何疑问,请检查此Omniauth-facebook。
我只能通过在 devise.rb 中添加这个来检索电子邮件:
config.omniauth :facebook, "KEY", "SECRET", scope: 'email', info_fields: 'email, name'
确保您没有在信息字段请求中放置空格(另一个答案可能打错了这个)。
您需要特别要求从信息字段哈希中返回电子邮件,因为默认情况下并非如此。
如果您在不使用 Devise 的情况下使用 Omniauth-Facebook,请在 config/initializers/omniauth.rb 文件中进行如下设置:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, Rails.application.secrets.omniauth_provider_key, Rails.application.secrets.omniauth_provider_secret,
:scope => 'email', :display => 'popup', :info_fields => 'name,email'
end
此信息有点隐藏在 omniauth-facebook gem 的 GitHub 自述文件 Configuring 部分的末尾。
使用 Devise plus Omniauth,但是,您可以像这样在 config/initializers/devise.rb 中设置它(字符串中没有空格!):
config.omniauth :facebook, 'app_id', 'app_secret', scope: 'email', info_fields: 'name,email'
我遇到了同样的问题。我让它可以更新 omniauth 和 omniauth-facebook gems。
我遇到了同样的问题,但事实证明这是一个不同的问题,如果您使用客户端流程,请确保在您的客户端上请求您需要的范围:
FB.login(function(response) {
// hit API callback endpoint
}, {scope: 'email'});