当用户选择使用linkedin取消登录我的应用程序时如何处理?
当我点击取消时,我被重定向到以下网址: 本地主机:9393 / AUTH / LinkedIn /回调oauth_problem = user_refused
其中显示以下错误消息: OAuth :: / auth / linkedin / callback出现问题 parameter_absent
如果用户选择取消使用Linkedin登录,我只想将用户重定向到主页。
# ************************************************
# Oauth using Omniauth methods
# ************************************************
%w(get post).each do |method|
send(method, "/auth/:provider/callback") do
"<pre>" + env['omniauth.auth'].inspect + "</pre>"
end
end
ENV['LINKEDIN_CONSUMER_KEY'] = "xxxxxxx"
ENV['LINKEDIN_CONSUMER_SECRET'] = "xxxxxxxx"
use OmniAuth::Builder do
provider :linkedin, ENV['LINKEDIN_CONSUMER_KEY'], ENV['LINKEDIN_CONSUMER_SECRET'], :scope => 'r_fullprofile+r_emailaddress+r_network', :fields => ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location", "connections"]
end
get '/auth/failure' do
flash[:notice] = params[:message] # if using sinatra-flash or rack-flash
redirect '/'
end
答案 0 :(得分:0)
在rails应用程序中,我通过将以下内容放入config / initializers / omniauth.rb解决了同样的问题。请注意,我重定向到特定的auth失败路由,以便我可以提供一些有关使用Linkedin登录的提示:
OmniAuth.config.on_failure do |env|
message_key = env['omniauth.error.type']
origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : ""
strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : ""
extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : ""
new_path = "/auth_failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}#{extra_params}"
Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish
end
在此处找到更多信息: How to rescue OmniAuth::Strategies::OAuth2::CallbackError?