Rails上的open_id_authentication 3

时间:2011-01-14 18:17:52

标签: ruby-on-rails-3 openid

我正在将应用程序从Rails 2迁移到Rails 3。 该应用程序使用Open ID登录Google,我正在使用open_id_authentication插件。

代码看起来像这样:

options = {
  :identifier => 'https://www.google.com/accounts/o8/id',
  :required => [ 'http://axschema.org/contact/email', 
                 'http://axschema.org/namePerson/first',
                 'http://axschema.org/namePerson/last' ], 
  :oauth => { 
     :consumer =>  OAUTH_CONSUMER_TOKEN,
     :scope => "http://www.google.com/m8/feeds/" 
  } 
} 
authenticate_with_open_id('https://www.google.com/accounts/o8/id',  options)
   do |result, identity_url, registration, extended_attributes|

   email = registration["http://axschema.org/contact/email"]

end

这在Rails 2上运行正常,但是当我更新到Rails 3并更新Open ID插件时(出于兼容性原因),当我尝试获取email属性时,我收到以下错误:

SessionsController中的ArgumentError #create      http://schema.openid.net/contact/email不是定义的简单注册字段

ruby-openid (2.1.8) lib/openid/extensions/sreg.rb:32:in `check_sreg_field_name'
ruby-openid (2.1.8) lib/openid/extensions/sreg.rb:266:in `[]'
app/controllers/sessions_controller.rb:33:in `open_id_authentication'
vendor/plugins/open_id_authentication/lib/open_id_authentication.rb:114:in `complete_open_id_authentication'
vendor/plugins/open_id_authentication/lib/open_id_authentication.rb:90:in `authenticate_with_open_id'

此错误意味着什么,我该如何解决?我知道通信工作正常,因为我收到了Google的页面并被要求输入密码,但我无法弄清楚为什么在更新到Rails 3时电子邮件属性停止了。

1 个答案:

答案 0 :(得分:0)

找到答案here。有关如何获取电子邮件AX属性的代码如下:

options = {
  :identifier => 'https://www.google.com/accounts/o8/id',
  :required => [ 'http://axschema.org/contact/email', 
                 'http://axschema.org/namePerson/first',
                 'http://axschema.org/namePerson/last' ], 
  :oauth => { 
     :consumer =>  OAUTH_CONSUMER_TOKEN,
     :scope => "http://www.google.com/m8/feeds/" 
  } 
} 
authenticate_with_open_id('https://www.google.com/accounts/o8/id',  options)
   do |result, identity_url, registration, extended_attributes|

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

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

end