Gem [不再处理] 关于 ‘weibo 登陆的相关 gems 和使用方法’ 的问题

neilalaer · 2012年05月22日 · 最后由 neilalaer 回复于 2012年05月24日 · 3524 次阅读

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

补充信息:rails 3.2.3, ruby 1.9.3

匿名 #2 2012年05月22日

恩 你做好把例子给我看看 哈哈

我正打算做这个

provider :tsina, '<APPKEY>', '<APP_SECRET>'

这段是让你配置你申请的 APP_KEY 和 APP_SECRET 不带尖括号

我确实配置了申请的 key 和 secret key,此处只是用来替代明文。

我现在的进度是: 修改 oa-oauth (0.2.6) lib/omniauth/strategies/oauth.rb 大约第 14 行#initilize 方法体中,改supersuper(app, options). 避免上面提到 ArgumentError; 现在在处理<%= link_to '微博登陆', '/auth/tsina' %>的问题,参考教程里没有在 route 里配置的,我想是其他机制吧?

因为从来没有接触过 Rack,现在慢慢开始学。

最上面 omniauth 报错是说参数个数不对。

原始的 gem 是没有问题的,你要从自己的代码上面找原因,你改 gem 的代码可不行

谢谢提醒!好吧,我再看看。

我分别参考以下两篇博客进行 rails 配置,还没有写什么功能代码。 http://jimdrannbauer.com/2011/01/29/rails-3-rspec-cucumber-jquery-devise-mongoid-and-compass/ http://www.haojii.com/2011/09/rails-oauth-with-sina-weibo-tutorial/

匿名 #8 2012年05月22日

#7 楼 @neilalaer omniauth_china 不是说已经 move 到 omniauth 里面了吗

@sharp 你说的对,谢谢指正!

我之前改 Gemfile,属于 trial-n-error;对于网上看到过的“omniauth_china 已经 move 到 omniauth 里面”也没有提起注意 (我真的不知道什么时候开始,Gemfile 开始成为 best practice 的)。

Gemfile 可以改为

gem 'omniauth'
gem 'oa-oauth' , '>= 0.3.2'

哎呀,那个参数过多的问题又出来了 0_0i

匿名 #10 2012年05月22日

你查查 default_options 里面的参数有哪些 -> OmniAuth::Strategy::Options

见原贴头部更新的内容。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号