現在、私はレール4のプロジェクトに取り組んでいますが、今は別のアプリケーション(ssoではなくAPIにアクセスするため)をリンク/接続する必要があります。例.com。 (注:example.comは3本足のOAuthセキュリティアーキテクチャを使用しています)"認証に失敗しました:無効な認証:OAuth2 ::エラー"カスタムのomniauth戦略
私はomniouth戦略を実行するために有することを見出し検索した後。このため
私はthisリンクを査読しています。 Strategy-Contribution-Guideとしてセットアップを完了してフェーズをリクエストすることができます。ここでサンプルコードを見つけることができます。
require 'multi_json'
require 'omniauth/strategies/oauth2'
require 'uri'
module OmniAuth
module Strategies
class MyAppStrategy < OmniAuth::Strategies::OAuth2
option :name, 'my_app_strategy'
option :client_options, {
site: site_url,
authorize_url: authorize_url,
request_url: request_url,
token_url: token_url,
token_method: :post,
header: { Accept: accept_header }
}
option :headers, { Accept: accept_header }
option :provider_ignores_state, true
def consumer
binding.pry
::OAuth::Consumer.new(options.client_id, options.client_secret, options.client_options)
end
def request_phase # rubocop:disable MethodLength
binding.pry
request_token = consumer.get_request_token({:oauth_callback => callback_url}, options.request_params)
session["oauth"] ||= {}
session["oauth"][name.to_s] = {"callback_confirmed" => request_token.callback_confirmed?, "request_token" => request_token.token, "request_secret" => request_token.secret}
if request_token.callback_confirmed?
redirect request_token.authorize_url(options[:authorize_params])
else
redirect request_token.authorize_url(options[:authorize_params].merge(:oauth_callback => callback_url))
end
rescue ::Timeout::Error => e
fail!(:timeout, e)
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
fail!(:service_unavailable, e)
end
def callback_phase # rubocop:disable MethodLength
fail(OmniAuth::NoSessionError, "Session Expired") if session["oauth"].nil?
request_token = ::OAuth::RequestToken.new(consumer, session["oauth"][name.to_s].delete("request_token"), session["oauth"][name.to_s].delete("request_secret"))
opts = {}
if session["oauth"][name.to_s]["callback_confirmed"]
opts[:oauth_verifier] = request["oauth_verifier"]
else
opts[:oauth_callback] = 'http://localhost:3000/auth/callback' #callback_url
end
@access_token = request_token.get_access_token(opts)
super
rescue ::Timeout::Error => e
fail!(:timeout, e)
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
fail!(:service_unavailable, e)
rescue ::OAuth::Unauthorized => e
fail!(:invalid_credentials, e)
rescue ::OmniAuth::NoSessionError => e
fail!(:session_expired, e)
end
def custom_build_access_token
binding.pry
verifier = request["oauth_verifier"]
client.auth_code.get_token(verifier, get_token_options(callback_url), deep_symbolize(options.auth_token_params))
end
alias_method :build_access_token, :custom_build_access_token
def raw_info
binding.pry
@raw_info ||= access_token.get('users/me').parsed || {}
end
private
def callback_url
options[:redirect_uri] || (full_host + script_name + callback_path)
end
def get_token_options(redirect_uri)
{ :redirect_uri => redirect_uri }.merge(token_params.to_hash(:symbolize_keys => true))
end
end
end
エンド
(あなたが知っていたか聞いてきますので、答えは私が持っている私は、にできるリダイレクトexample.com思いもログインした後、私は私のcallback_phaseに戻ることができていますフローを確認する方法はにcallback_phaseを追加しました。
しかし、戦略を実行した後、私はエラー以下
ERRORを取得しています - omniauth:(my_app_strategy)認証失敗を! invalid_credentials:OAuth2 :: Error。
デバッグ後、super
コール(callback_phaseメソッドから)でこのエラーが発生しています。
最初に私けれどもが、いくつかの資格情報の問題があるが、私は、私はbuild_access_token
れるエラーを取得しています詳細については、また(super
呼び出しの前に実行されている)は、次の
@access_token = request_token.get_access_token(opts)
を使用してアクセストークンを取得することができる午前かもしれ
あなたはより多くの情報のため thisリンクを参照することができのOAuth2方法である(ちょうどページのbuild_access_tokenを検索してください)。
EDIT - 1
request methodからこの問題を得ることがわかったデバッグ後。 (ファラデーリクエストの作成中)。不適切なOAuthコンシューマの資格情報 - ここでは、コードスニペットが
response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
yield(req) if block_given?
end
であるここに私のファラデー要求は、私は、エラーメッセージ
HTTPステータス400以下になっています応答で
#<struct Faraday::Request method=:post, path="example.com/oauth/access_token", params={}, headers={"User-Agent"=>"Faraday v0.9.2", "Content-Type"=>"application/x-www-form-urlencoded"}, body={"grant_type"=>"authorization_code", "code"=>"aPexxxvUg", "client_id"=>"xxxxxur303GXEch7QK9k", "client_secret"=>"xxxxxxcad97b3d252e2bcdd393a", :redirect_uri=>"http://localhost:3000/auth/my_app_strategy/callback"}, options=#<Faraday::RequestOptions (empty)>>
です。
ので、いずれかのヘルプは、この問題を修正することができますか?
アクセストークンを格納するための他の方法は、通信目的でこれを利用できるようにしてください。 おかげで、すべての
ここでは、コンシューマーキーとコンシューマーシークレットを公開しています。私はそれらを編集しようとしましたが、編集がライブになる前に他人が確認する必要があります。これらの資格情報をすぐに取り消し、新しい資格情報を取得してください。 –
@ScottS。あなたの返事をありがとう、私はそれを世話します。それらは乱数です。 –
oauthの設定にhttps://github.com/intridea/omniauth-oauth2/blob/master/lib/omniauth/strategies/oauth2.rbを使用しなかった理由はありますか? –