Update 使用 omniauth-weibo-oauth2 gem,仍在尝试中。
Update ( aborted!) 根据新形势下的认证服务 omniauth 的安排,对于 weibo 登陆需求(oauth 版本,不是 oauth2)的功能,可以使用 omniauth-weibo.不再打算使用旧版本。
参考步骤:
在下想通过 omniauth gems 来访问新浪微博的数据接口。有个问题没有解决,希望能得到一些提示。先谢谢了。两个问题如下,
问题一:是否配置正确,或者使用正确的 rubygems, 下面 ArgumentError 是什么原因呢? Gemfile 中相关的 gems
gem 'omniauth'
gem 'oa-oauth' , '>= 0.3.2'
config\initializers\omniauth.rb
require 'oa-oauth'
Rails.application.config.middleware.use OmniAuth::Builder do
provider :tsina, '<APPKEY>', '<APP_SECRET>'
end
报错信息
ArgumentError (Received wrong number of arguments. [:tsina, "<APP_KEY>", "<APP_SECRET>", {:site=>"http://api.t.sina.com.cn", :request_token_path=>"/oauth/request_token", :access_token_path=>"/oauth/access_token", :authorize_path=>"/oauth/authorize", :realm=>"OmniAuth"}]):
  omniauth (1.1.0) lib/omniauth/strategy.rb:145:in `initialize'
  oa-oauth (0.2.6) lib/omniauth/strategies/oauth.rb:14:in `initialize'
  omniauth_china (0.1.1) lib/omniauth_china/strategies/tsina.rb:27:in `initialize'
  rack (1.4.1) lib/rack/builder.rb:82:in `new'
  rack (1.4.1) lib/rack/builder.rb:82:in `block in use'
  rack (1.4.1) lib/rack/builder.rb:130:in `[]'
  rack (1.4.1) lib/rack/builder.rb:130:in `block in to_app'
  rack (1.4.1) lib/rack/builder.rb:130:in `each'
  rack (1.4.1) lib/rack/builder.rb:130:in `inject'
  rack (1.4.1) lib/rack/builder.rb:130:in `to_app'
  omniauth (1.1.0) lib/omniauth/builder.rb:48:in `call'
  mongoid (2.4.10) lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
...... <omitted>
问题二:报错信息显示 omniauth 抛出异常错误,但是没看懂相关代码,求解释。问题键代码注释标识 Q:
# https://github.com/intridea/omniauth/blob/master/lib/omniauth/strategy.rb line 124 to line 139
def initialize(app, *args, &block)
      @app = app
      @options = self.class.default_options.dup
      options.deep_merge!(args.pop) if args.last.is_a?(Hash)  #Q: args有6个参数,此处处理入参数组末尾的一个元素。
      options.name ||= self.class.to_s.split('::').last.downcase
      self.class.args.each do |arg|     # Q: self.class.args 调用类方法,约line 81,但最后返回的是空数组;为什么不是这样`self.class.args(args).each do |arg|; ...;end`调用?
        options[arg] = args.shift
      end
      # Make sure that all of the args have been dealt with, otherwise error out.
      raise ArgumentError, "Received wrong number of arguments. #{args.inspect}" unless args.empty?  #其他还有5个入参是怎么处理的?
      yield options if block_given?
    end
# 详细代码见:https://github.com/intridea/omniauth/blob/master/lib/omniauth/strategy.rb
module ClassMethods
      # Returns an inherited set of default options set at the class-level
      # for each strategy.
      def default_options
        return @default_options if @default_options
        existing = superclass.respond_to?(:default_options) ? superclass.default_options : {}
        @default_options = OmniAuth::Strategy::Options.new(existing)
      end
     def args(args = nil)
        @args = Array(args) and return if args
        existing = superclass.respond_to?(:args) ? superclass.args : []
        return @args || existing
      end
end