如何在 Rails 2 中正确使用 OmniAuth

发布于 2024-11-24 01:18:45 字数 1354 浏览 2 评论 0原文

我正在尝试为我的应用程序使用 OmniAuth (0.2.6) 策略。架构为 Rails 2.3.10,Rack 版本为 1.1(OmniAuth 需要此版本或更高版本)。出现的问题是 Rails 无法识别我重定向到“/auth/provider”。实际的错误消息是“没有路由匹配“/auth/casport”。即使抛出该错误,代码似乎也会执行到此错误为止:“request.env['omniauth.auth']”,其中我很确定这意味着它无法识别 env 哈希的“omniauth.auth”密钥,我感觉 OmniAuth 没有正确加载,我已经使用 Mongrel 测试了相同的策略和代码库。 Webrick,我收到相同的错误消息。

这是我的 OmniAuth 初始值设定项 (config/initializers/omniauth.rb):

ActionController::Dispatcher.middleware.use OmniAuth::Builder do
  provider :casport, :setup => true
end

我确实在 auth/casport/setup 和 auth/casport/callback 的位置有路由,

我也尝试过这种方法。 :怎么做您在 Rails 2.2 上实现了 OmniAuth?

config.middleware.use OmniAuth::Builder do
  provider :casport, :setup => true
end

config/environments/development.rb 文件中,

有人对我可能做错的事情有任何想法吗?谢谢!

通过将该中间件代码放入 > 我也尝试过这种方法 有人用过吗omn​​iauth 与 Rails 2.3.8?

ActionController::Dispatcher.middleware.use OmniAuth::Strategies::Casport = {
  :setup => true
}

并返回读取错误:“NoMethodError:#Hash 的未定义方法“新”...

编辑 2:我已升级到 Rails 2.3。 10,因为 OmniAuth 与 Rails 2.3.4 不兼容。

I am trying to use an OmniAuth (0.2.6) strategy for my application. The architecture is Rails 2.3.10 and Rack is version is 1.1 (this version or higher is required for OmniAuth). A problem that occurs is Rails doesn't recognize my redirect to "/auth/provider". The actual error message is "No route matches "/auth/casport". Even though that error gets thrown, the code seems to execute past that point up to this error: "request.env['omniauth.auth']", which I'm pretty sure means it doesn't recognize the "omniauth.auth" key for the env hash. I have a feeling that OmniAuth isn't being loaded properly. I've tested the same strategy and code base with Mongrel and Webrick, and I get identical error messages.

Here is my OmniAuth initializer (config/initializers/omniauth.rb):

ActionController::Dispatcher.middleware.use OmniAuth::Builder do
  provider :casport, :setup => true
end

I definitely have routes in places for auth/casport/setup and auth/casport/callback.

I've also tried this approach: How do you implement OmniAuth on Rails 2.2?

config.middleware.use OmniAuth::Builder do
  provider :casport, :setup => true
end

by placing that middleware code in the config/environments/development.rb file.

Does anyone have any thoughts on what I could be doing incorrectly? Thanks!

Edit: I've also tried this approach Has anyone used omniauth with rails 2.3.8?

ActionController::Dispatcher.middleware.use OmniAuth::Strategies::Casport = {
  :setup => true
}

and that returns a read error: "NoMethodError: undefined method 'new' for #Hash...

Edit 2: I've upgraded to Rails 2.3.10 as OmniAuth isn't compatible with Rails 2.3.4.

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

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

发布评论

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

评论(1

抽个烟儿 2024-12-01 01:18:45

我在 OmniAuth 的 Github 页面 https://github.com/intridea/omniauth/issues/397 创建了一个问题,有了上述详细信息,这是 OmniAuth 的创建者 Michael Bleigh 的回应:

“OmniAuth 从未正式支持 Rails 2.3.x ——我听说有些人很幸运,但这不是保证的事情!”

所以看起来我上面发布的方法可能对某些人有用。为了保证正确运行,需要Rails 3。

I created an issue on OmniAuth's Github page https://github.com/intridea/omniauth/issues/397, with the above details and this is the response from Michael Bleigh, the creator of OmniAuth:

"OmniAuth has never officially supported Rails 2.3.x -- I've heard that some people have had luck with it but it's not something that is guaranteed. Sorry!"

So it looks like the methods I posted above may work for some folks. To guarantee correct operation, Rails 3 is needed.

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