OmniAuth 路由错误,没有路由匹配

发布于 2025-01-02 21:27:55 字数 744 浏览 3 评论 0原文

我正在研究 Ryan Bates Railscast #235 OmniAuth Part 1,使用 OmniAuth gem 允许用户使用 Twitter 或 Facebook 以及后来的 Google Apps 登录我的 Web 应用程序。

现在我遇到了这个错误,

Routing Error

No route matches [GET] "/auth/twitter"

我已经正确设置了routes.rb 文件来处理身份验证回调提供程序匹配,如下所示:

  match "/auth/:provider/callback" => "authentications#create"

当我链接到 localhost:3000/auth/twitter 时,我收到此错误。贝茨在 -07:36 的 Railscast 中。

这个问题的可能解决方案是什么?会是routes.rb的问题吗?或者omniauth.rb?

我们的omniauth.rb 看起来像这样:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'OURCONSUMERKEY', 'OURCONSUMERSECRET'
  provider :twitter,  'OURCONSUMERKEY', 'OURCONSUMERSECRET'
end

I am working through Ryan Bates railscast #235 OmniAuth Part 1, using the OmniAuth gem to allow users to sign in to my web app using Twitter or Facebook and later Google Apps.

Right now I am encountering this error

Routing Error

No route matches [GET] "/auth/twitter"

I have correctly set up my routes.rb file to handle the auth callback provider match like so:

  match "/auth/:provider/callback" => "authentications#create"

When i link to localhost:3000/auth/twitter, i get this error. where as Bates in his Railscast at -07:36.

What could be a possible solution to this issue? Would it be an issue with routes.rb? or omniauth.rb?

Our omniauth.rb looks like this:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'OURCONSUMERKEY', 'OURCONSUMERSECRET'
  provider :twitter,  'OURCONSUMERKEY', 'OURCONSUMERSECRET'
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

清欢 2025-01-09 21:27:55

您需要在 Devise gem 使用的模型中注释掉 ':omniauthable' (通常是模型 'User' = user.rb 文件):

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable,
         :rememberable, :trackable, :validatable # plus whatever other calls...
       # :omniauthable

  [...]

end

使用 ':omniauthable' 调用意味着加载 devise/omniauth 组件(这会导致冲突使用您的omniauth 设置)。

You need to comment out ':omniauthable' in your model used by the Devise gem (usually it's the model 'User' = the user.rb file):

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable,
         :rememberable, :trackable, :validatable # plus whatever other calls...
       # :omniauthable

  [...]

end

Using the ':omniauthable' call means loading devise/omniauth components (which cause conflicts with your omniauth setup).

明媚殇 2025-01-09 21:27:55

仅供参考,如果您遇到此问题并且将 Devise 2.1.x 与 OmniAuth 1.x 和 OAuth2 结合使用,请注意,现在的最佳实践是使用 /users/auth/facebook (即是,控制器中的一个目录名为“users/”)...

因此,您需要点击 /users/auth/facebook,尽管几乎所有教程, OmniAuth 的示例和指南说点击 /auth/facebook!这(再加上 Facebook 不会更新我的网站 URL,直到我添加端口 # :3000、保存、传播并点击它,然后再次删除它)让我难住了一段不为人知的时间,以保护懊恼的人。 :-)

另外,与现在获得最多票数的答案不同——这当然解决了问题,但并没有允许您与 Devise 集成——我不需要从 Devise 中删除 :omniauthable (一旦我点击了正确的 URL)。当我使用错误的 URL 时,它只会对我“造成冲突”。

编辑:另外,与最初的问题不同,使用 Devise 2.1.x 和 OmniAuth 1.x,据我所知,不需要为 Rack- 创建一个 omniauth.rb 命名初始化程序- 使用 Devise,您只需将 OmniAuth 位添加到 config/initializers/devise.rb 中(但不能 100% 确定这一点)。请参阅 Facebook 示例plataformatec/devise 的 OmniAuth 概述顶部的 Strong> 部分,了解更多详细信息。

fyi, if you're encountering this issue and you're combining Devise 2.1.x with OmniAuth 1.x and OAuth2, be aware that best practice now is to use /users/auth/facebook (that is, a directory in your controllers called, 'users/') ...

Accordingly, you'll need to hit /users/auth/facebook, even though almost all the tutorials, examples, and guides out there for OmniAuth say to hit /auth/facebook! This (in combination with the fact that Facebook wouldn't update my Site URL until I added the port # :3000, saved, propagated & hit it, then removed it again) had me stumped for a period of time that shall remain untold, to protect the chagrined. :-)

Also, unlike the answer with most votes right now--which of course solves the problem, but doesn't allow you to integrate with Devise--I didn't need to remove :omniauthable from Devise (once I was hitting the correct URL). It only 'causes conflicts' for me when I was using the wrong URL.

EDIT: Also, unlike in the original question, with Devise 2.1.x and OmniAuth 1.x, as far as I know, one doesn't need to create an omniauth.rb named initializer for Rack--with Devise, you just add your OmniAuth bits to config/initializers/devise.rb (but not 100% sure about this). See plataformatec/devise's OmniAuth Overview under Facebook example section at top, for more detail.

等数载,海棠开 2025-01-09 21:27:55

实际上,omniauth 负责定义 Twitter 的路由。

因此添加此代码仅用于回调

match "/auth/twitter/callback" => "sessions#create"
match "/signout" => "sessions#destroy", :as => :signout

尝试重新启动服务器:rails server

Actually, omniauth takes care of defining routes for twitter.

So adding this code is only for the callback

match "/auth/twitter/callback" => "sessions#create"
match "/signout" => "sessions#destroy", :as => :signout

Try restarting your server : rails server

月依秋水 2025-01-09 21:27:55

Twitter 上指定应用程序的回调 URL 应该可以解决此问题。

Specifying the Callback URL for the app on Twitter should resolve this.

北恋 2025-01-09 21:27:55

我在 Rails 2.3.16 上使用omniauth 1.1.3 时也遇到了同样的问题。当在 webrick 下运行时,它在开发中运行良好,但是当在 Fastcgi 下运行时,omniauth 提供程序无法检测到任何路由。

问题是 fastcgi 代码没有正确填充 PATH_INFO 环境变量,而omniauth 依赖于此。

解决方案是添加另一个中间件来修复 PATH_INFO。我使用了这个:

class Rack::PathInfoRewriter
  def initialize(app)
    @app = app
  end

  def call(env)
    env.delete('SCRIPT_NAME')
    parts = env['REQUEST_URI'].split('?')
    env['PATH_INFO'] ||= parts[0]
    env['QUERY_STRING'] ||= parts[1].to_s
    @app.call(env)
  end
end

注意 ||= 这是必要的,以便 webrick 继续在开发模式下正常工作。

I have seen the same problem when using omniauth 1.1.3 on Rails 2.3.16. It worked fine in development when running under webrick, but when running in production under Fastcgi the omniauth providers fail to detect any of the routes.

The problem was that the fastcgi code wasn't populating the PATH_INFO environmment variable correctly, and omniauth is depending on that.

The solution was to add another middleware to fix up PATH_INFO. I used this:

class Rack::PathInfoRewriter
  def initialize(app)
    @app = app
  end

  def call(env)
    env.delete('SCRIPT_NAME')
    parts = env['REQUEST_URI'].split('?')
    env['PATH_INFO'] ||= parts[0]
    env['QUERY_STRING'] ||= parts[1].to_s
    @app.call(env)
  end
end

Note the ||= it was necessary so that webrick continued to work OK in development mode.

§对你不离不弃 2025-01-09 21:27:55
match '/auth/:provider/callback' => 'sessions#auth_callback', :as => :auth_callback

它在我的项目中有效,你可以尝试这样

match '/auth/:provider/callback' => 'sessions#auth_callback', :as => :auth_callback

it works in my project, you can have a try like this

陈甜 2025-01-09 21:27:55

SB,我可以提个建议吗?首先查找第 241 集。这是比较简单的情节。我倾向于认为你的问题不在于routes.rb。我正在使用 OmniAuth 来验证用户身份并代表我的用户发送推文,而我拥有的该部分的唯一路由是:

 match "/auth/twitter/callback" => "sessions#create"
 match "/signout" => "sessions#destroy", :as => :signout

SB, May I make a suggestion? Look up episode #241 first. It is simpler episode. I tend to think your issue is NOT with routes.rb. I am using OmniAuth to authenticate users and to send tweets on behalf of my users and the only routes I have for that part is:

 match "/auth/twitter/callback" => "sessions#create"
 match "/signout" => "sessions#destroy", :as => :signout
小草泠泠 2025-01-09 21:27:55

我也有同样的问题。我缺少的部分是将以下内容放入 Gem 文件中,

gem 'devise'

当我运行 bundle install 并刷新页面时,它已修复。

I had this same problem. The part that I was missing was putting the following in the Gem File

gem 'devise'

When I ran bundle install and refreshed the page it was fixed.

树深时见影 2025-01-09 21:27:55

请务必在 config/initializers/* 中添加“omniauth.rb

我的 config/initializers/omniauth.rb 如下所示

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET
end

Be sure to add your 'omniauth.rb' in the config/initializers/*

My config/initializers/omniauth.rb looks like this

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET
end
孤独患者 2025-01-09 21:27:55

gem 'omniauth-twitter' 添加到您的 Gemfile,重新运行 bundle,然后重新启动您的 Web 服务器。在 Rails 4.0 之前,我相信您必须将 gem 行添加到 :assets 组中。

Add gem 'omniauth-twitter' to your Gemfile, re-run bundle, and restart your web server. Prior to Rails 4.0, I believe you have to add the gem line to an :assets group.

拥抱没勇气 2025-01-09 21:27:55

以下是我对此问题的解决方案:

如果您从 Nginx 进行代理:

location @rails {
    proxy_set_header Host $http_host;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://rails_app;
  }

将旧版本的omniauth 添加到您的 Gemfile

#auth
gem 'omniauth-facebook', '~> 8.0'
gem 'omniauth', '~> 1.9.1' #this is important

设置强制 SSL

config.force_ssl = ENV['CLIENT_URL'].include?("https")

相关答案 < a href="https://stackoverflow.com/a/66651142/788798">https://stackoverflow.com/a/66651142/788798

Here is my fix to this problem:

If you proxying from Nginx:

location @rails {
    proxy_set_header Host $http_host;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://rails_app;
  }

Add an older version of omniauth to your Gemfile

#auth
gem 'omniauth-facebook', '~> 8.0'
gem 'omniauth', '~> 1.9.1' #this is important

Set force SSL

config.force_ssl = ENV['CLIENT_URL'].include?("https")

Related answer https://stackoverflow.com/a/66651142/788798

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文