1

I've been working with the Facebook Omniauth and Devise but have run into an issue where the returned hash does not contain any of the information I'm requesting in my info fields. I'm just trying to get Facebook to return the first and last names with the email if it's available separately so I can create new users with that information. I don't think I need to set up specific permissions with Facebook to return these fields (as you do with some info_fields that require verification).

I am using Rails 4.2.6. This is the returned hash:

#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash 
expires=true expires_at=1486542909 
token="EAAXYor9I0XgBADbIaZC99Lu0xGAdVVSXSyZC1TsZCHcZCi0kiWPdZCHA0DsLait7zn1cSpEsbc1mViWvFxZAPraz2Hdyfyn2pfonpeDlhP6Nqv7RiGNpDQ1Bxxm81LHwoPHMIbag95pksGZChcQ7YMrk8eUuQNkgScZD"> 
extra=#<OmniAuth::AuthHash 
raw_info=#<OmniAuth::AuthHash id="xxxxxxxxxx" name="xxxxxxxx">>
info=#<OmniAuth::AuthHash::InfoHash 
image="http://graph.facebook.com/xxxxxxxxxx/picture?type=square" 
name="xxxxxxxxxx"> 
provider="facebook" 
uid="xxxxxxxxxx">

Most of the solutions for similar questions have involved something in devise.rb, which looks like this for me:

config.omniauth :facebook, "xxxIDxxx", "xxxSECRETxxx", scope: 'email,public_profile', info_fields: 'email, first_name, last_name'

My CallBacksController:

class CallbacksController < Devise::OmniauthCallbacksController

def all
    @user = User.from_omniauth(request.env["omniauth.auth"])
    @request = request.env["omniauth.auth"]

    if @user.persisted?
        flash.notice = "You have successfully signed in!"
        sign_in_and_redirect @user
    else
        session["devise.user_attributes"] = @user.attributes
        redirect_to new_user_registration_path      
    end
end

alias_method :facebook, :all
end

My User.rb:

    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

has_many :posts

def self.from_omniauth(auth)
      puts auth
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.email = auth.info.email || ""
        user.password = Devise.friendly_token[0,20]
     end
end

And these are the related gems I'm using:

gem 'geocoder'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'

This has been driving me crazy for the past few days and I'm stuck. Any suggestions would be greatly appreciated. If there are things that might be related that I accidentally left out, let me know and I'd be glad to post them. Thanks!

  • any update on this, I have the same issue, only i am just using omniauth-facebook gem and no Devise – Phil Jan 02 '17 at 14:42
  • Unfortunately not. This was only a side project I was working on and decided to focus on other parts of the project and never got back to figuring this out. If you do find the solution though, please let me know! Thanks! – Michael Radzwilla Jan 03 '17 at 15:29
  • It's not an actual fix for this issue, but as somewhat of a workaround, I started using the Koala gem for this app and after logging in with the facebook-omniauth gem I am able to use Koala to get the specific pieces of information that I was trying to get from the auth hash. These two resources helped me figure it out https://www.youtube.com/watch?v=E_XACDrZSiI&t=138s and http://stackoverflow.com/questions/34808971/gem-koala-facebook-api-get-object-request – Michael Radzwilla Jan 08 '17 at 12:01

1 Answers1

0

I use the omniauth gem and the omniauth-facebook gem. The hash that I get is rather long. And this is how I create new users:

  #Facebook login
  def self.from_omniauth(auth)
    where(email: auth.info.email).first_or_initialize.tap do |user|
      user.name = auth.info.name
      user.email = auth.info.email
      user.password = auth.uid
      user.password_confirmation = auth.uid
      user.remote_avatar_url = auth.info.image
      user.uid = auth.uid
      user.provider = auth.provider
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at) if auth.provider == "facebook"
      user.save!
    end
  end

To debug this in that time, I raised an error when I got the hash in my controller:

raise request.env["omniauth.auth"].to_yaml

This gives you in the terminal a nicely formatted output of the hash you are getting from facebook.

Alexander Luna
  • 5,261
  • 4
  • 30
  • 36