簡體   English   中英

omn​​iauth facebook rails 4無法獲取性別

[英]gender not fetch by omniauth facebook rails 4

我正在使用omniauth-facebook gem。 我需要從Facebook提取性別和電子郵件,但這沒有發生。 有人可以幫我從這里出去嗎?

用戶模型

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

用戶控制器

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

omn​​iauth.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

您可以從extra.raw_info.gender獲取性別屬性。 像下面

user.gender = auth.extra.raw_info.gender

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM