使用Google OpenID进行Ruby open_id_authentication

我是在我的Rails应用程序中实现OpenID的第一步。 open_id_authentication似乎是一个相当容易使用的插件,这就是我决定使用它的原因。

使用我的Google帐户登录似乎完美无缺,但是我没有获得我需要的sreg / AX字段。 我的代码目前如下:

class SessionsController  ["http://axschema.org/contact/email"]) do |result, identity_url, registration| if result.successful? p registration.data @current_user = User.find_by_identity_url(identity_url) if @current_user successful_login else failed_login "Sorry, no user by that identity URL exists (#{identity_url})" end else failed_login result.message end end end private def successful_login session[:user_id] = @current_user.id redirect_to(root_url) end def failed_login(message) flash[:error] = message redirect_to(new_session_url) end end 

我已经阅读了有关Google OpenID的各种讨论,并且只是说您需要使用AX架构而不是sreg字段email ,但即使我这样做(您可以在上面的代码中看到),registration.data也会保持空( {} )。

如何通过open_id_authentication有效地要求大多数OpenID提供商提供的电子邮件?

authenticate_with_open_id返回Sreg对象,而不是AX响应。 所以你需要使用Rack :: OpenID :: REPONSE来实现这样的响应:

 ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE]) 

您可以获取数据后

 ax_response['http://axschema.org/contact/email'] ax_response['http://axschema.org/namePerson/first'] ax_response['http://axschema.org/namePerson/last'] 

我还为Ruby on Rails 3,OpenID和Google拼凑了一个完整的解决方案: http : //blog.sethladd.com/2010/09/ruby-rails-openid-and-google.html

这篇文章包含了一个很好的策略,可以将AX用于google,将Sreg用于其他人,以便更加无缝地实现这一目标http://www.franzens.org/2009/01/using-google-federated-login-in-your。 HTML