这个代码绝对有效,当我有"是"宝石的版本。
我大量使用了这个:https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview#google-oauth2-example
我最近需要升级。
我尝试使用google_oauth2来验证用户的Gmail地址。如果它与预先批准的用户列表匹配,那么我想将它们视为已授权,如果不是,则不然。这是一个非常基本的设置。
这是我的设置。
应用程序/模型/ user.rb ```
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :omniauthable, :omniauth_providers => [:google_oauth2]
```
配置/ routes.rb中 ```
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks" }
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def new_session_path(scope)
new_user_session_path
end
end
```
我得到的错误是:
Showing /private/var/www/concerto-signage/vendor/bundle/ruby/2.1.0/gems/devise-3.2.4/app/views/devise/sessions/new.html.erb where line #3 raised:
undefined method `session_path' for #<#<Class:0x007fa2e4ae54f0>:0x007fa2e4aefea0>
Extracted source (around line #3):
1
2
3
4
5
6
<h2>Sign in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
我不能得到的是,为什么设法试图去看那个观点?我只是想让它创建current_user并从那里获取它?
另请注意,我甚至没有包含omniauth_callbacks控制器,因为它此时甚至不会触发(基于我已经制作的一些调试语句)
我正在检查日志,我注意到了这一点:
Started GET "/users/auth/google_oauth2/callback?state=029f48fd5d35243b095c6d6933c7699d6501f104a47b23a0&code=4/6SKMFVh
dZLHRRD-2GiIsyNXLNh-t.AhDB-ii59RcVsjMf6whcw_4NFnKMjwI" for 127.0.0.1 at 2014-08-11 13:56:52 -0400
Processing by OmniauthCallbacksController#failure as HTML
答案 0 :(得分:3)
经过一系列的故障排除后,我终于明白了。
在config / initializers / devise.rb中,我不得不改变我的config.omniauth调用以包含策略类。 config.omniauth :google_oauth2,
'APP_ID',
'APP_SECRET',
:strategy_class => OmniAuth::Strategies::GoogleOauth2