omn​​iauth facebook范围只能收到电子邮件

时间:2014-12-12 12:28:54

标签: ruby-on-rails facebook-graph-api ruby-on-rails-4 scope omniauth

我希望能够访问范围信息。我唯一可以访问的是插入数据库的电子邮件信息。我确实发现user_birthday是一个有效的,但是当我登录时它不会获取它。任何人都可以告诉我如何查看facebook发送到范围的信息

需要帮助。

的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 '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

用户表

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

当我查看数据库日志时,这就是我得到的

----------- 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", "xxxxxx@gmail.com"], ["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 ---------------------

我希望能够在这里使用current_user.birthday吗?

感谢

1 个答案:

答案 0 :(得分:1)

尝试在范围后指定,选项 info_fields 如下:

# 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'