omniauth-facebook not hash not containing all data

124 views Asked by At

I am trying to get up a login and create user via omniauth-facebook gem, I am getting the hash back but it doesnt have all the data that I need and I running out of things to try, setup is below:

All I want to get is first_name, last_name, email and bio

Sessions Controller

 def create
   render text: request.env['omniauth.auth'].to_json
 end

Route

  get     '/auth/:provider/callback', to: 'sessions#create'

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, Rails.application.secrets.facebook_app_id,
    Rails.application.secrets.facebook_app_secret, 
    :scope => 'email,public_profile', :info_fields =>
    'name,email,first_name,last_name,bio'
  provider :twitter,  Rails.application.secrets.twitter_app_id,
    Rails.application.secrets.twitter_app_secret
end

The returned hash:

{
  "provider": "facebook",
  "uid": "1**************",
  "info": {
    "name": "Philip Davies",
    "image": "http://graph.facebook.com/1*************/picture?type=square"
  },
  "credentials": {
    "token": "************",
    "expires_at": 1488480906,
    "expires": true
  },
  "extra": {
    "raw_info": {
      "name": "Philip Davies",
      "id": "1***********"
    }
  }
}
1

There are 1 answers

1
Michael Radzwilla On

I had this same issue and was unable to solve it but ended up finding a workaround via the Koala gem (https://github.com/arsduo/koala). I ended up using the facebook-omniauth gem to log in, saved that authentication token, and then used Koala to get the specific information from the user's Facebook profile.

My user.rb looks like this:

def self.from_omniauth(auth)
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.password = Devise.friendly_token[0,20]
        user.oauth_token = auth.credentials.token
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)
        @user_info = user.facebook.get_object(:me, { fields: [:first_name, :last_name, :email]})
        user.first_name = @user_info['first_name']
        user.last_name = @user_info['last_name']
        user.email = @user_info['email']
     end
end

def facebook
    Koala::Facebook::API.new(oauth_token)
end

I know it's not the perfect solution, but it allowed me to do what I was trying to do, so maybe it will work for you as well. If you have any questions just let me know.