前提
sorcery
でtwitter認証をしようとして、 401 Unauthorized
というエラーが発生しました.
process_callback(params, session)
メソッド内のget_access_token(args)
エラーが発生してることが分かり、session
が取得できてないことがエラーの原因かと考えました。どのようにすれば取得できるか教えて頂きたいです。よろしくお願いします。
実現したいこと
- Twitterアカウントでのログイン
発生している問題・エラーメッセージ
OAuth::Unauthorized - 401 Unauthorized: app/controllers/oauths_controller.rb:14:in `callback'
該当のソースコード
ruby
1class OauthsController < ApplicationController 2 skip_before_action :require_login, raise: false 3 4 # sends the user on a trip to the provider, 5 # and after authorizing there back to the callback url. 6 def oauth 7 login_at(params[:provider]) 8 end 9 10 def callback 11 provider = params[:provider] 12 debugger 13 if @user = login_from(provider) 14 redirect_to root_path, notice: "#{provider.titleize}でログインしました" 15 else 16 begin 17 @user = create_from(provider) 18 # NOTE: this is the place to add '@user.activate!' if you are using user_activation submodule 19 20 reset_session # protect from session fixation attack 21 auto_login(@user) 22 redirect_to root_path, notice: "#{provider.titleize}でログインしました" 23 rescue 24 redirect_to root_path, alert: "#{provider.titleize}でのログインに失敗しました" 25 end 26 end 27 end 28 29 #example for Rails 4: add private method below and use "auth_params[:provider]" in place of 30 #"params[:provider] above. 31 32 private 33 34 def auth_params 35 params.permit(:code, :provider) 36 end 37end
ruby
1class Authentication < ApplicationRecord 2 belongs_to :user 3end
ruby
1class User < ApplicationRecord 2 authenticates_with_sorcery 3 has_many :authentications, dependent: :destroy 4 accepts_nested_attributes_for :authentications
ログイン画面
ruby
1 = link_to 'Login with Twitter', auth_at_provider_path(provider: :twitter)
config/initializers/sorcery.rb
ruby
1Rails.application.config.sorcery.submodules = [:external] 2 3# Here you can configure each submodule's features. 4Rails.application.config.sorcery.configure do |config| 5 config.external_providers = [:twitter] 6 config.twitter.key = Rails.application.credentials.twitter[:key] 7 config.twitter.secret = Rails.application.credentials.twitter[:secret] 8 config.twitter.callback_url = "http://127.0.0.1:3000/oauth/callback?provider=twitter" 9 config.twitter.user_info_mapping = { name: 'name' } 10 config.user_config do |user| 11 user.stretches = 1 if Rails.env.test? 12 user.authentications_class = Authentication 13 end 14 config.user_class = 'User' 15end
routes.rb
post "oauth/callback", to: "oauths#callback" get "oauth/callback", to: "oauths#callback" get "oauth/:provider", to: "oauths#oauth", as: :auth_at_provider
マイグレーションファイル
class SorceryExternal < ActiveRecord::Migration[6.1] def change create_table :authentications do |t| t.integer :user_id, null: false t.string :provider, :uid, null: false t.timestamps null: false end add_index :authentications, [:provider, :uid] end end
試したこと
callback
アクション内の処理を一つずつ追ってみたところlogin_from
メソッド内sorcery_fetch_user_hash(provider_name)
メソッドの@access_token ||= @provider.process_callback(params, session)
のprocess_callback(params, session)
メソッド内のget_access_token(args)
でエラーが出ていることが分かりました。
(ruby) get_access_token(args) eval error: 401 Unauthorized /Users/apple/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/oauth-0.5.10/lib/oauth/consumer.rb:257:in `token_request' /Users/apple/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/oauth-0.5.10/lib/oauth/tokens/request_token.rb:26:in `get_access_token' /Users/apple/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/sorcery-0.16.3/lib/sorcery/protocols/oauth.rb:27:in `get_access_token' (rdbg)//Users/apple/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/sorcery-0.16.3/lib/sorcery/providers/twitter.rb:1:in `process_callback' nil
process_callback(params, session)
メソッドは下記のようになっており、sessionが取得できてないことがエラーの原因かと考えました。
def process_callback(params, session) args = { oauth_verifier: params[:oauth_verifier], request_token: session[:request_token], request_token_secret: session[:request_token_secret] } args[:code] = params[:code] if params[:code] get_access_token(args) end (rdbg) params #<ActionController::Parameters {"provider"=>"twitter", "oauth_token"=>"jMbf4QAAAAABf4hhAAABgqCFNmY", "oauth_verifier"=>"C12AVt3OEYL0sIz3VHot6TNVU5gnb7L8", "controller"=>"oauths", "action"=>"callback"} permitted: false> (rdbg) session #<ActionDispatch::Request::Session:0x133648 not yet loaded> (ruby) session[:request_token] nil
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。