1

我想让用户使用 Google OpenID 登录我的 Rails 应用程序。所以我有这些宝石

gem 'omniauth'
gem 'omniauth-openid'
gem 'devise'

配置/初始化程序/omniauth.rb

require 'openid/store/filesystem'
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :open_id, :store => OpenID::Store::Filesystem.new('/tmp'), :name => 'google', 
    :identifier => 'https://www.google.com/accounts/o8/id', :require => 'omniauth-openid'
end

OmniAuth.config.on_failure = Proc.new do |env|
    OmniAuth::FailureEndpoint.new(env).redirect_to_failure 

结尾

页面上还有一个链接

<% if current_user %>
  Welcome <%= current_user.name %>!
  <%= link_to "Sign Out", signout_path %>
<% else %>
  <%= link_to "Sign in with Google", "/auth/google" %>

<% end %>

当我被重定向到谷歌登录页面时,我点击“允许”,我被重定向回 ... /auth/failure?message=invalid_credentials&strategy=google出于某种原因,尽管我登录正确。

这是来自 Webrick 的日志:

Started GET "/auth/google" for 127.0.0.1 at 2012-12-20 12:45:13 +0700
(google) Callback phase initiated.
Error attempting to use stored discovery information: OpenID::TypeURIMismatch
Attempting discovery to verify endpoint
Performing discovery on https://www.google.com/accounts/o8/id?id=323221212143243243
WARNING: making https request to https://www.google.com/accounts/o8/id?id=2121212143243243243243 without verifying server certificate; no CA path was specified.
(google) Callback phase initiated.
(google) Authentication failure! invalid_credentials encountered.


Started GET "/auth/google/callback?_method=post&openid.ns= .... very long string" for 127.0.0.1 at 2012-12-20 12:45:18 +0700


Started GET "/auth/failure?message=invalid_credentials&strategy=google" for 127.0.0.1 at 2012-12-20 12:45:18 +0700

我做错什么了?

4

1 回答 1

4

你说的并没有做错什么。不幸的是,Google 对 OpenID 的响应是一个非常长的 URL。只要它超过了 webrick 可以处理的 256 个字符。

如果您将另一个服务器添加到您的 gemfile,例如:

gem 'thin'

然后像这样启动服务器:

rails s thin

那么您应该会发现通过 google 登录就可以了。

于 2012-12-20T07:51:57.070 回答