Ruby on Rails Omniauth facebook does not return email address

I have been trying for several days to set up my Omniauth for facebook. I do not know what I am doing wrong.

I can not get the email address of the user. The returned hash contains only "name" and "uid" even "first_name" and "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' } 

Return Hash:

 #<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"> 
+6
source share
5 answers

I need to get an email by adding this to devise.rb:

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

Make sure you do not put spaces in the request information field (another answer probably sealed it).

You need to specifically request that the email message be returned from the hash of the information field, as it is not the default.

If you use Omniauth-Facebook without Devise, configure it in the config / initializers / omniauth.rb file as follows:

 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 

This information is hidden at the very end of the Configuring section of the omniauth-facebook gemhub file.

With Devise plus Omniauth, however, you set it to config / initializers / devise.rb like this (without spaces in the line!):

 config.omniauth :facebook, 'app_id', 'app_secret', scope: 'email', info_fields: 'name,email' 
+10
source

I had the same problem. I made it work by updating omniauth and omniauth-facebook gems.

+5
source

You need to request permission. For example, to request permissions for email , user_birthday and read_stream and display the authentication page in a popup 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 

Please check Omniauth-facebook if you have any doubts.

+2
source

I had the same problem, but it turned out that it was another problem, if you are using a client-side thread, remember to request the areas you need on your client:

 FB.login(function(response) { // hit API callback endpoint }, {scope: 'email'}); 
+1
source

All Articles