2010-03-22 46 views
1

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

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

class SessionsController < ApplicationController 

    def new; end 

    def create 
    open_id_authentication 
    end 


    protected 
    def open_id_authentication 
     authenticate_with_open_id(params[:openid_identifier], :required => ["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 

我已经了解谷歌OpenID的各种讨论和所有只能说,你需要需要AX架构,而不是SREG场email,但即使当我这样做(如您在上面的代码中看到的),registration.data将保持为空({})。

如何有效地要求来自大多数OpenID提供者的open_id_authentication邮件?

回答

9

authenticate_with_open_id返回Sreg对象,而不是AX响应。因此,你需要实例化这与输入反应机架:: 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'] 
+0

谢谢TON!所附的自述文件提到您可以像Sreg字段一样添加AX,但是它没有提到您必须这样做! :)保存我的日子 – 2010-03-22 13:38:23

+0

我搜索很长时间才发现,两个星期前 – shingara 2010-03-22 14:07:40