【问题标题】:"No route matches [GET] "/auth/google_oauth2" error keeps coming up“没有路线匹配 [GET]”/auth/google_oauth2”错误不断出现
【发布时间】:2021-01-18 23:16:40
【问题描述】:

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

查看页面链接: <%= 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

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

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

铁路路线:

                               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

【问题讨论】:

  • 您是否将get '/auth/:provider/callback' => 'sessions#omniauth' 添加到config/routes.rbroutes.rb 文件确定当有人访问您的应用程序中的路径时会发生什么(在本例中为 /auth/google_oauth2)。错误告诉你 Rails 在路由列表中找不到任何告诉它该做什么的东西。
  • 感谢您的回复,是的,我的路线中确实有这个问题,并且出现了同样的错误。我刚刚尝试使用 GitHub 登录进行设置,但实际上我得到了同样的无路由匹配错误......还有其他东西可以做到这一点吗?我的omniauth版本?添加要发布的路线信息。
  • 您能否运行rails routes 并将结果添加到您的问题中?
  • 添加到我的帖子的rails路线结果中。
  • 另外,需要注意的是,我认为,我刚刚完成了使用 github 设置omniauth 的过程,并且出现了同样的“无路由匹配获取”错误...

标签: ruby-on-rails google-oauth omniauth


【解决方案1】:

取决于OmniAuth 版本(2.0.0):

OmniAuth 现在默认只使用 POST 作为允许的 request_phase 方法

添加

# 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

考虑CVE remediation described here

【讨论】:

  • 是的,我能够重现原始问题并通过转换为 POST 来解决它。但请注意,对于 OmniAuth > 2.0.0,您还必须安装 omniauth-rails_csrf_protection gem,并且链接的 CVE (~> 0.1) 中提到的版本不能完全适用于最新的 OmniAuth 版本。使用该版本你会得到一个OmniAuth::AuthentictyError。相反,请改用 'omniauth-rails_csrf_protection', '~> 1.0'。
  • 更新了帖子,使基于omniauth版本更加清晰。
  • @SarahMarie 似乎根据omniauth docs,即使omniauth >= 2.0,仍然需要使用omniauth-rails_csrf_protection 以防止CSRF。 github.com/omniauth/omniauth/wiki/Upgrading-to-2.0#rails 我认为新的omniauth 版本更改了默认配置,因此不会创建GET 路由。但是CSRF的实际处理还是在omniauth-rails_csrf_protection
  • 我怎么知道我是否“还需要GET请求”?我有一个单独的 Nuxt.js 前端和 Rails API,我的登录流程涉及打开一个弹出窗口 (window.open),其 URL 指向通往 Omniauth 端点 /auth/google_oauth2 的 Rails 路由。我看不到在这种情况下使用POST 的方法。有吗?还是我需要GET的情况?
  • 我还发现我需要omniauth-rails_csrf_protection 才能使其与omniauth >= 2.0 一起使用。如果你能更新你的答案,我相信它会帮助人们节省一些时间。
【解决方案2】:

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了解更多信息。

【讨论】:

    【解决方案3】:

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

    登录链接

    <%= 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 %>
    

    链接方法明明是:post但是报错get route丢失

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

    <%= javascript_pack_tag 'application' %>
    

    【讨论】:

      猜你喜欢
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 2016-06-15
      • 2011-12-11
      • 2015-09-27
      相关资源
      最近更新 更多