mardi 29 septembre 2015

gender not fetch by omniauth facebook rails 4

I am using omniauth-facebook gem. I need to fetch gender and email from facebook, but it is not happening. Can anyone help me out here?

User Model

class User < ActiveRecord::Base
    attr_accessible :email, :name, :uid, :provider, :gender, :oauth_token, :oauth_expires_at    
    validates_presence_of :name, :uid
    validates_uniqueness_of :uid    

    def self.from_omniauth(auth)
      where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|       
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.email = auth.info.email
        user.gender = auth.info.gender
        user.image = auth.info.image            
        user.save!
      end
    end
end

User Contoller

class UsersController < ApplicationController
respond_to :html, :json
def create
@user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = @user.id    
  redirect_to api_browsing_screen_url       
end  
end

omniauth.rb

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
if Rails.env.production?
  provider :facebook, 'app_id', 'app_secret_key'
elsif Rails.env.development?
  provider :facebook, 'app_id', 'app_secret_key', {:scope => 'publish_actions,email', :client_options => { :ssl => { :ca_file => "#{Rails.root}/config/ca-bundle.crt" }}}
else  
  provider :facebook, 'app_id', 'app_secret_key'
end  
end

Aucun commentaire:

Enregistrer un commentaire