Cyrus Zei
Cyrus Zei

Reputation: 2660

omniauth facebook scope only get e-mail

I want to be able to access the scope information. The only thing that I can access is the email information that are being inserted to the database. I did find that user_birthday is a valid one, but when I login it wont fetch it. Can anybody also tell me how to see what info facebook is sending to the scope

Need help here.

ApplicationController

helper_method :current_user

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

OmniAuth.rb (path = config/initalizers/)

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'xxxxxxxxx', 'xxxxxxxxx',
    :scope => "email, user_birthday", 
    :display => 'popup',
    :client_options => {
    :site => 'https://graph.facebook.com/v2.0',
    :authorize_url => "https://www.facebook.com/v2.0/dialog/oauth"
}

end

gem file

gem 'omniauth'
gem 'omniauth-facebook', '~> 1.4.1'

user.rb (path = models/)

class User < ActiveRecord::Base
  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.name = auth.info.name
    user.oauth_token = auth.credentials.token
    user.oauth_expires_at = Time.at(auth.credentials.expires_at)
    user.email = auth.info.email
    user.birthday = auth.info.user_birthday
    user.save!
  end
end

    def largeimage
        "http://graph.facebook.com/#{self.uid}/picture?type=large"
    end
    def normalimage
        "http://graph.facebook.com/#{self.uid}/picture?type=normal"
    end


end

user table

create_table "users", force: true do |t|
    t.string   "provider"
    t.string   "uid"
    t.string   "email"
    t.string   "birthday"
    t.string   "name"
    t.string   "oauth_token"
    t.datetime "oauth_expires_at"
    t.datetime "created_at"
    t.datetime "updated_at"
end

and when I look at the databse log this is what I get

-----------db log-----------------------

User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."provider" = 'facebook' AND
"users"."uid" = 'xxxxxxxx'  ORDER BY "users"."id" ASC LIMIT 1
(1.4ms)  begin transaction
SQL (1.2ms)  INSERT INTO "users" ("created_at", "email", "name", "oauth_expires_at",
"oauth_token", "provider", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", 
"2014-12-12 12:11:38.281524"], ["email", "[email protected]"], ["name", "Cyrus Zei"], 
["oauth_expires_at", "2015-02-10 11:51:00.000000"], ["oauth_token", "xxxxxxxxxxxxxx"], 
["provider", "facebook"], ["uid", "xxxxxxxxxxx"], ["updated_at", "2014-12-12 12:11:38.281524"]]
(13.2ms)  commit transaction
(0.1ms)  begin transaction
(0.0ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 38ms (ActiveRecord: 16.4ms)

-------------db log---------------------

I want to be able to use current_user.birthday any help here ?

thanks

Upvotes: 0

Views: 976

Answers (1)

Ricardo Emerson
Ricardo Emerson

Reputation: 906

Try specify after scope, the option info_fields like bellow:

# Configuration option using Devise with omniauth-facebook.
config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], scope: "email, public_profile, user_birthday, user_location", info_fields: 'id, name, first_name, middle_name, last_name, age_range, link, gender, locale, timezone, updated_time, verified, email, birthday, location'

Upvotes: 2

Related Questions