问题描述
在my-app中添加twitter omniauth登录后,出现一条错误消息,提示“ RuntimeError(未找到resource_class)”。
由Api :: V1 :: Auth :: OmniauthCallbacksController#omniauth_success处理为HTML
参数:{“ provider” =>“ twitter”}
完成500内部服务器错误
RuntimeError(未找到resource_class):
app / controllers / api / v1 / auth / omniauth_callbacks_controller.rb:26:in get_resource_from_auth_hash' app/controllers/api/v1/auth/omniauth_callbacks_controller.rb:15:in
omniauth_success'
def get_resource_from_auth_hash
super
@resource.credentials = auth_hash["credentials"]
clean_resource
end
config>初始化程序> omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,ENV.fetch("TWITTER_KEY"),ENV.fetch("TWITTER_SECRET"),callback_url:
"http://localhost:3000/api/v1/auth/twitter/callback"
end
控制器> Api> V1>身份验证> OmniauthCallbacksController
module Api module V1
module Auth
class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController
skip_before_action :skip_session
def redirect_callbacks
super
end
def omniauth_success
super
update_auth_header
end
def omniauth_failure
super
end
protected
def get_resource_from_auth_hash
super
@resource.credentials = auth_hash["credentials"]
clean_resource
end
def render_data_or_redirect(message,data,user_data = {})
if Rails.env.production?
if ['inAppBrowser','newWindow'].include?(omniauth_window_type)
render_data(message,user_data.merge(data))
elsif auth_origin_url
redirect_to DeviseTokenAuth::Url.generate(auth_origin_url,data.merge(blank: true))
else
fallback_render data[:error] || 'An error occurred'
end
else
render json: @resource,status: :ok
end
end
def clean_resource
@resource.username = strip_emoji(@resource.username)
end
def strip_emoji(str)
str.encode('SJIS','UTF-8',invalid: :replace,undef: :replace,replace: '').encode('UTF-8')
end
end
end
end
end
route.rb
Rails.application.routes.draw do
namespace :api,defaults: { format: :json } do
scope :v1 do
mount_devise_token_auth_for 'User',at: 'auth',controllers: {
registrations: 'api/v1/auth/registrations',omniauth_callbacks: 'api/v1/auth/omniauth_callbacks'
}
end
end
end
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)