我在Windows 7 32位主机中使用Vagrant与框ubuntu/trusty32设置了Rails应用程序。该应用具有Google OAuth登录功能。 Gemfile中有以下的宝石吧:无法使用omniauth-google-oauth2登录,导致您正在重定向
gem 'omniauth'
gem 'omniauth-google-oauth2'
而且在/config/initializers/devise.rb
:
config.omniauth :google_oauth2, ENV["GOOGLE_APP_ID"], ENV["GOOGLE_APP_SECRET"]
OmniAuth.config.full_host = lambda do |env|
forwarded = env['HTTP_X_FORWARDED_FOR']
forwarded.blank? ? "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}" : "https://#{env['HTTP_HOST']}"
end
当我尝试登录,它停止在该网址看到“您将被重定向”旁边没有发生。
http://localhost:3000/omniauths/auth/google_oauth2/callback?state=xxxx&code=xxx
我得到了以下日志:
Started GET "/omniauths/auth/google_oauth2" for 10.0.2.2 at 2017-10-07 11:07:21 +0000
log writing failed. closed stream
I, [2017-10-07T11:07:21.507524 #2581] INFO -- omniauth: (google_oauth2) Request phase initiated.
Started GET "/omniauths/auth/google_oauth2/callback?state=xxxx&code=xxxx" for 10.0.2.2 at 2017-10-07 11:07:21 +0000
log writing failed. closed stream
I, [2017-10-07T11:07:22.155462 #2581] INFO -- omniauth: (google_oauth2) Callback phase initiated.
Processing by SessionController#google_oauth2 as HTML
log writing failed. closed stream
Parameters: {"state"=>"a6a1e7b1733c564b96ab650f360fbae63cff5bdc6a488f1a", "code"=>"4/D4aX9Bm9XQmSIm_RXBhBi1EXE1AHKp3Q-WVbSpNSdvE"}
log writing failed. closed stream
Omniauth Load (4.5ms) SELECT `omniauths`.* FROM `omniauths` WHERE `omniauths`.`provider` = 'google_oauth2' AND `omniauths`.`uid` = '110281880098696358918' LIMIT 1
log writing failed. closed stream
Permission::Organization Load (1.6ms) SELECT `permission_organizations`.* FROM `permission_organizations` WHERE `permission_organizations`.`domain` = 'gmail.com' LIMIT 1
log writing failed. closed stream
Redirected to http://localhost:3000/
log writing failed. closed stream
Completed 403 Forbidden in 302ms (ActiveRecord: 17.4ms)
的代码是不是我的,我不是一个红宝石专家了。所以我不知道为什么我被困在屏幕上。我还发现了一个控制器:
class SessionController < Devise::OmniauthCallbacksController
def google_oauth2
@omniauth = Omniauth.find_for_google(request.env['omniauth.auth'])
if @omniauth&.persisted?
@omniauth.user.last_sign_in = Time.now
@omniauth.user.save
flash[:notice] = 'I signed in with Google authentication'
sign_in_and_redirect @omniauth, event: :authentication
else
#redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
redirect_to '/', status: :forbidden
end
end
end
这里是config/routes.rb
Rails.application.routes.draw do
devise_for :omniauths, controllers: {
omniauth_callbacks: "session"
}
mount RailsAdmin::Engine => '/rails_admin', as: 'rails_admin'
post '/api', to: "api#post"
post '/error_catch', to: "api#error_catch"
post '/error_log', to: "api#error_log"
get '/file/:uuid/:filename', to: "api#file"
get '/svg_parts/*dxf_path', to: "svg#part"
post '/svg/project(.:format)'
post '/svg/requirement(.:format)'
get '/svg/test(.:format)'
get '/pdf/owner_estimate'
get '/pdf/builder_estimate'
get '/pdf/wholesale_estimate'
get '/api/perform_test'
get '/pdf/requirement'
root to: "angular#index"
get '/assets/*path', controller: 'application', action: 'handle_404'
get '*path', to: "angular#index"
end
的信息并不对这个问题看够了吗?我还可以提供可能有所帮助的更多信息。
让我们试试这个=>'''devise_for:用户,控制器:{omniauth_callbacks: “会话”}''' –
@RonanLouarn我得到了一个资源错误'映射omniauth_callbacks不是omniauthable(引发ArgumentError)' – Sithu
也许你错过了在你的模型中写'''devise:omniauthable,omniauth_providers:[:google_oauth2]'''@Sithu –