いつもお世話になっております。
omniauth-twitterとdeviseを使用しTwitterを活用した認証を実装しているのですが、下記の例のhashにてextra以下の情報が取得できず困っております。(credentialまでしか返ってきません)
{ :provider => "twitter", :uid => "123456", :info => { :nickname => "johnqpublic", :name => "John Q Public", :location => "Anytown, USA", :image => "http://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", :description => "a very normal guy.", :urls => { :Website => nil, :Twitter => "https://twitter.com/johnqpublic" } }, :credentials => { :token => "a1b2c3d4...", # The OAuth 2.0 access token :secret => "abcdef1234" }, :extra => { :access_token => "", # An OAuth::AccessToken object :raw_info => { :name => "John Q Public", :listed_count => 0, :profile_sidebar_border_color => "181A1E", :url => nil, :lang => "en", :statuses_count => 129, :profile_image_url => "http://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", :profile_background_image_url_https => "https://twimg0-a.akamaihd.net/profile_background_images/229171796/pattern_036.gif", :location => "Anytown, USA", :time_zone => "Chicago", :follow_request_sent => false, :id => 123456, :profile_background_tile => true, :profile_sidebar_fill_color => "666666", :followers_count => 1, :default_profile_image => false, :screen_name => "", :following => false, :utc_offset => -3600, :verified => false, :favourites_count => 0, :profile_background_color => "1A1B1F", :is_translator => false, :friends_count => 1, :notifications => false, :geo_enabled => true, :profile_background_image_url => "http://twimg0-a.akamaihd.net/profile_background_images/229171796/pattern_036.gif", :protected => false, :description => "a very normal guy.", :profile_link_color => "2FC2EF", :created_at => "Thu Jul 4 00:00:00 +0000 2013", :id_str => "123456", :profile_image_url_https => "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", :default_profile => false, :profile_use_background_image => false, :entities => { :description => { :urls => [] } }, :profile_text_color => "666666", :contributors_enabled => false } } }
extra以下を取得するにはどのようにすればよろしいのでしょうか?
下記がコードです。
app/models/user.rb
ruby
1class User < ApplicationRecord 2 # userが存在しなければ、新規で登録する 3 unless user 4 ActiveRecord::Base.transaction do 5 user = User.register_user(auth) 6 # 一旦twitterだけで登録できるように実装 7 user.sns_providers.create( 8 type: auth["provider"], 9 follower: auth["extra"]["raw_info"]["followers_count"], 10 account_name: auth["info"]["nickname"], 11 link: auth["info"]["urls"]["Twitter"] 12 ) 13 end 14 end 15 16 user 17 end 18 19 # userテーブルに登録 20 def self.register_user(auth) 21 user = User.create( 22 name: auth["info"]["name"], 23 email: User.dummy_email(auth), 24 password: Devise.friendly_token[0, 20], 25 # デフォルト値を代入する 26 genre_id: "0", 27 telephone: "000-0000-0000" 28 ) 29 end 30end 31
app/controllers/omniauth_callbacks_controller.rb
ruby
1module Users 2 class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController 3 include Devise::Controllers::Rememberable 4 5 def omniauth_success 6 get_resource_from_auth_hash 7 # create_token_info 8 set_token_on_resource 9 create_auth_params 10 11 sign_in(:user, @resource, store: false, bypass: false) 12 13 if @resource.save! 14 # update_token_authをつけることでレスポンスヘッダーに認証情報を付与できる。 15 update_auth_header 16 yield @resource if block_given? 17 render json: @resource, status: :ok 18 else 19 render json: { message: "failed to login" }, status: 500 20 end 21 end 22 23 protected 24 def get_resource_from_auth_hash 25 # find or create user by provider and nickname 26 @user = User.find_for_oauth(auth_hash) 27 28 # userが新規登録か調べる 29 if @user.new_record? 30 @oauth_registration = true 31 end 32 33 # assign any additional (whitelisted) attributes 34 extra_params = whitelisted_params 35 @user.assign_attributes(extra_params) if extra_params 36 37 @user 38 end 39 end 40end
どなたかご教示いただけますと嬉しいです。
よろしくお願い致します。
あなたの回答
tips
プレビュー