使用omniauth-twitter gem将TypeError“无法将nil转换为String”

时间:2013-11-30 09:47:26

标签: ruby-on-rails twitter oauth omniauth

我正在使用rails 3.2.1 omn​​iauth-twitter(0.0.9)

但是在twitter回调阶段,我收到了这个错误:

TypeError "can't convert nil into String"

> omniauth-twitter (0.0.9) lib/omniauth/strategies/twitter.rb:22:in `+'
omniauth-twitter (0.0.9) lib/omniauth/strategies/twitter.rb:22:in `block in <class:Twitter>'
omniauth (1.1.4) lib/omniauth/strategy.rb:106:in `instance_eval'
omniauth (1.1.4) lib/omniauth/strategy.rb:106:in `block in compile_stack'
omniauth (1.1.4) lib/omniauth/strategy.rb:105:in `each'
omniauth (1.1.4) lib/omniauth/strategy.rb:105:in `inject'
omniauth (1.1.4) lib/omniauth/strategy.rb:105:in `compile_stack'
(eval):7:in `info_stack'
omniauth (1.1.4) lib/omniauth/strategy.rb:322:in `info'
omniauth (1.1.4) lib/omniauth/strategy.rb:335:in `auth_hash'
omniauth (1.1.4) lib/omniauth/strategy.rb:362:in `callback_phase'
omniauth-oauth (1.0.1) lib/omniauth/strategies/oauth.rb:58:in `callback_phase'
omniauth (1.1.4) lib/omniauth/strategy.rb:226:in `callback_call'
omniauth (1.1.4) lib/omniauth/strategy.rb:182:in `call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.1.4) lib/omniauth/builder.rb:49:in `call'

在下面添加twitter.rb代码:

> require 'omniauth-oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    class Twitter < OmniAuth::Strategies::OAuth
      option :name, 'twitter'
      option :client_options, {:authorize_path => '/oauth/authenticate',
                               :site => 'https://api.twitter.com'}

      uid { access_token.params[:user_id] }

      info do
        {
          :nickname => raw_info['screen_name'],
          :name => raw_info['name'],
          :location => raw_info['location'],
          :image => raw_info['profile_image_url'],
          :description => raw_info['description'],
          :urls => {
            'Website' => raw_info['url'],
            'Twitter' => 'http://twitter.com/' + raw_info['screen_name'], # 22 line no
          }
        }
      end

      extra do
        { :raw_info => raw_info }
      end

      def raw_info
        @raw_info ||= MultiJson.decode(access_token.get('/1/account/verify_credentials.json').body)
      rescue ::Errno::ETIMEDOUT
        raise ::Timeout::Error
      end

      alias :old_request_phase :request_phase

      def request_phase 
        screen_name = session['omniauth.params']['screen_name']
        if screen_name && !screen_name.empty?
          options[:authorize_params] ||= {}
          options[:authorize_params].merge!(:force_login => 'true', :screen_name => screen_name)
        end
        old_request_phase
      end


    end
  end
end

我已经更新了omniauth-twitter gem,它仍然是上面提到的相同版本。

可能的原因是什么?

1 个答案:

答案 0 :(得分:3)

因为raw_info['screen_name']值为nil,你不能将它与字符串连接起来。

'http://twitter.com/' + raw_info['screen_name'], # 22 line no替换为

"http://twitter.com/#{raw_info['screen_name']}",