"No route matches [GET] "/auth/google_oauth2”错误不断出现

"No route matches [GET] "/auth/google_oauth2" error keeps coming up

错误信息: “没有路线匹配 [GET] “/auth/google_oauth2”

查看页面link: <%= link_to "Log In with Google", "/auth/google_oauth2" %>

相关宝石:

gem 'omniauth'
gem 'dotenv-rails'
gem 'omniauth-google-oauth2'

初始化文件:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"],ENV["GOOGLE_CLIENT_SECRET"], skip_jwt: true
end

我以此为指导:Medium Guide

我已经在 Developer Tools 上完成了两次设置应用程序的过程,以获取客户端 ID 和密码,以防出现问题,但两次我都遇到了同样的错误。我是编码新手,所以我确定这是非常愚蠢和明显的事情,但我似乎无法找到它。 感谢您的帮助!

编辑: 我的路线文件中也有: get '/auth/:provider/callback' => 'sessions#omniauth'

Rails 路线:

                               Prefix Verb   URI Pattern                                                                              Controller#Action
                                 root GET    /                                                                                        sessions#home
                                      GET    /auth/:provider/callback(.:format)                                                       sessions#omniauth
                               signup GET    /signup(.:format)                                                                        users#new
                                      POST   /signup(.:format)                                                                        users#create
                                login GET    /login(.:format)                                                                         sessions#new
                                      POST   /login(.:format)                                                                         sessions#create
                               logout DELETE /logout(.:format)                                                                        sessions#destroy
                          user_groups GET    /users/:user_id/groups(.:format)                                                         groups#index
                                      POST   /users/:user_id/groups(.:format)                                                         groups#create
                       new_user_group GET    /users/:user_id/groups/new(.:format)                                                     groups#new
                                users GET    /users(.:format)                                                                         users#index
                                      POST   /users(.:format)                                                                         users#create
                             new_user GET    /users/new(.:format)                                                                     users#new
                            edit_user GET    /users/:id/edit(.:format)                                                                users#edit
                                 user GET    /users/:id(.:format)                                                                     users#show
                                      PATCH  /users/:id(.:format)                                                                     users#update
                                      PUT    /users/:id(.:format)                                                                     users#update
                                      DELETE /users/:id(.:format)                                                                     users#destroy
                                      GET    /users(.:format)                                                                         users#index
                                      POST   /users(.:format)                                                                         users#create
                                      GET    /users/new(.:format)                                                                     users#new
                                      GET    /users/:id(.:format)                                                                     users#show
                           edit_group GET    /groups/:id/edit(.:format)                                                               groups#edit
                                group GET    /groups/:id(.:format)                                                                    groups#show
                                      DELETE /groups/:id(.:format)                                                                    groups#destroy
                           categories GET    /categories(.:format)                                                                    categories#index
                             category GET    /categories/:id(.:format)                                                                categories#show
                                cards GET    /cards(.:format)                                                                         cards#index
                                      POST   /cards(.:format)                                                                         cards#create
                             new_card GET    /cards/new(.:format)                                                                     cards#new
                            edit_card GET    /cards/:id/edit(.:format)                                                                cards#edit
                                 card GET    /cards/:id(.:format)                                                                     cards#show
                                      PATCH  /cards/:id(.:format)                                                                     cards#update
                                      PUT    /cards/:id(.:format)                                                                     cards#update
                                      DELETE /cards/:id(.:format)                                                                     cards#destroy
        rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                  action_mailbox/ingresses/postmark/inbound_emails#create
           rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                     action_mailbox/ingresses/relay/inbound_emails#create
        rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                  action_mailbox/ingresses/sendgrid/inbound_emails#create
  rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#health_check
        rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#create
         rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                              action_mailbox/ingresses/mailgun/inbound_emails#create
       rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#index
                                      POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#create
    new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                             rails/conductor/action_mailbox/inbound_emails#new
   edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                        rails/conductor/action_mailbox/inbound_emails#edit
        rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#show
                                      PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                      rails/conductor/action_mailbox/reroutes#create
                   rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
            rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
                   rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
            update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
                 rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

取决于 OmniAuth 版本 (2.0.0):

OmniAuth now defaults to only POST as the allowed request_phase method

添加:

# Gemfile
gem 'omniauth-rails_csrf_protection', '~> 0.1'

将链接更改为 POST 请求:

link_to "Log In with Google", "/auth/google_oauth2", method: :post
# or
button_to "Log In with Google", "/auth/google_oauth2"

如果您仍然需要 GET 个请求,请添加 security concern:

# config/initializers/omniauth.rb or similar
OmniAuth.config.allowed_request_methods = [:post, :get]

对于 omniauth < 2.0.0

考虑 CVE remediation described here

我遇到了这个确切的错误,除了它在我的主要布局上工作正常但在着陆页布局上不起作用(我有 2 种不同的布局)

签到link

<%= link_to '/auth/google_oauth2', method: :post, :class => "btn-get-started font-weight-semibold" do %>
  <span class="text-pink-400">
    <i class="icon-cart2 mr-2"></i> Order
  </span>
<% end %>

link方法明明是一个:post但是报错报错get route

通过确保着陆页布局具有所有必需的 javascript 来解决此问题

<%= javascript_pack_tag 'application' %>

OmniAuth 现在默认仅使用 POST 允许的 request_phase 方法。

您需要将所有 get 请求更新为 post 请求。

在您的视图文件中,您可以将锚标记替换为表单并提交按钮

<a href='/auth/developer'>Login with Developer</a>

以上代码可以替换为:-

<%= form_tag('/auth/developer', method: 'post') do %>
  <button type='submit'>Login with Developer</button>
<% end %>

如果您不想使用 post 方法,您可以添加以下代码:

config/initializers/omniauth.rb

OmniAuth.config.allowed_request_methods = [:post, :get]
OmniAuth.config.silence_get_warning = true

查看 here 了解更多信息。