【问题标题】:Using BOSS api with rails将 BOSS api 与 rails 一起使用
【发布时间】:2013-09-12 11:50:37
【问题描述】:

我正在尝试将 yahoo 的 BOSS api 与 rails 一起使用。
控制器:

class WelcomeController < ApplicationController
  def index
    require 'bossman'
    BOSSMan.application_id = "api key"

    boss = BOSSMan::Search.web("prospect park", :count => 5, :filter => "-hate")

    puts "Matches:"
    puts
    boss.results.each { |result| puts "#{result.title} [#{result.url}]" }
  end
end

在 gem 文件中我已经包含

gem 'gemcutter'

gem 'bossman','~> 0.4.1'

gem 'fakeweb'

gem 'spec'

gem 'activesupport'

当我运行应用程序时,我收到以下错误:

No such file or directory - getaddrinfo
Extracted source (around line #6):  

BOSSMan.application_id = ""
boss = BOSSMan::Search.images("brooklyn dumbo", :dimensions => "large") #Line 6
boss.results.map { |result| result.url }
end

【问题讨论】:

    标签: ruby-on-rails yahoo-boss-api


    【解决方案1】:

    经过长时间的调试,我终于设法让boss api通过rails工作。它并没有真正完善,但我的猜测是每个人都应该能够使用它。

    是这样的:

    require 'oauth_util.rb'
    require 'net/http'
     
    def get_response(buckets, query_params)
    
    
        parsed_url = URI.parse("https://yboss.yahooapis.com/ysearch/#{buckets}" )
    
        o = OauthUtil.new
        o.consumer_key = YAHOO_KEY
        o.consumer_secret = YAHOO_SECRET
        o.sign(parsed_url,query_params)
      
    
    
        Net::HTTP.start( parsed_url.host, parsed_url.port, :use_ssl => true ) { | http |
            req = Net::HTTP::Get.new ("/ysearch/#{buckets}?format=json&q=#{query_params}")
            
            req['Authorization'] = o.header
            response = http.request(req)
            return response.read_body
        }
    end

    使用我修改后的 oauth_util.rb 版本

    # A utility for signing an url using OAuth in a way that's convenient for debugging
    # Note: the standard Ruby OAuth lib is here http://github.com/mojodna/oauth
    # License: http://gist.github.com/375593
    # Usage: see example.rb below
    
    require 'uri'
    require 'cgi'
    require 'openssl'
    require 'base64'
    
    class OauthUtil
      
      attr_accessor :consumer_key, :consumer_secret, :token, :token_secret, :req_method, 
                    :sig_method, :oauth_version, :callback_url, :params, :req_url, :base_str
      
      def initialize
        @consumer_key = ''
        @consumer_secret = ''
        @token = ''
        @token_secret = ''
        @req_method = 'GET'
        @sig_method = 'HMAC-SHA1'
        @oauth_version = '1.0'
        @callback_url = ''
        @time
      end
      
      # openssl::random_bytes returns non-word chars, which need to be removed. using alt method to get length
      # ref http://snippets.dzone.com/posts/show/491
      def nonce
        Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first
      end
      
      def percent_encode( string )
        
        # ref http://snippets.dzone.com/posts/show/1260
        return URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") ).gsub('*', '%2A')
      end
    
      # @ref http://oauth.net/core/1.0/#rfc.section.9.2
      def signature
        key = percent_encode( @consumer_secret ) + '&' + percent_encode( @token_secret )
     
        # ref: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks
        digest = OpenSSL::Digest.new( 'sha1' )
       
        hmac = OpenSSL::HMAC.digest( digest, key, @base_str )
    
        # ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81
        Base64.encode64( hmac ).chomp.gsub( /\n/, '' )
        
      end
    
    
      
      # sort (very important as it affects the signature), concat, and percent encode
      # @ref http://oauth.net/core/1.0/#rfc.section.9.1.1
      # @ref http://oauth.net/core/1.0/#9.2.1
      # @ref http://oauth.net/core/1.0/#rfc.section.A.5.1
    
    
    
      def query_string
        pairs = []
        @params.sort.each { | key, val | 
          pairs.push( "#{ percent_encode( key ) }=#{ percent_encode( val.to_s ) }" )
        }
        pairs.join '&'
      end
      
      
      def header 
       'OAuth oauth_version="1.0",oauth_nonce="'+@nonce_now+'",oauth_timestamp="'+@time+'",oauth_consumer_key="'+@consumer_key+'",oauth_signature_method="'+@sig_method+'",oauth_signature="'+percent_encode(signature)+'"'
      end
        
      # organize params & create signature
      def sign( parsed_url, query_param )
            @time=Time.now.to_i.to_s
            @nonce_now=nonce
    
        @params = {
          'format' => 'json',      
          'oauth_consumer_key' => @consumer_key,
          'oauth_nonce' => @nonce_now,
          'oauth_signature_method' => @sig_method,
          'oauth_timestamp' => @time,
          'oauth_version' => @oauth_version,
          'q'=> query_param
        }
    
        # if url has query, merge key/values into params obj overwriting defaults
     
         #if parsed_url.query
        #  @params.merge! CGI.parse( parsed_url.query )
        #end
    
    
        # @ref http://oauth.net/core/1.0/#rfc.section.9.1.2
        @req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path
      
    
    
        # create base str. make it an object attr for ez debugging
        # ref http://oauth.net/core/1.0/#anchor14
    
        @base_str = [ 
          @req_method, 
          percent_encode(req_url), 
          # normalization is just x-www-form-urlencoded
          percent_encode(query_string)
          
        ].join( '&' )
    
        # add signature
    
        
        return self
      end
    end

    【讨论】:

      【解决方案2】:

      您可以尝试在控制器顶部包含 Socket 库。

      require 'socket'
      

      这似乎是 BOSS 的一些套接字问题。

      【讨论】:

      • 我查看了他们的存储库,它大约有 4 年的历史。我不认为他们正在做任何工作,因此有任何支持。有依赖 gems 要求是旧版本。
      • 哦!行。有没有办法将 yahoo boss api 与 rails 集成?
      • 您可能会在这里得到一些支持vinsol.com/blog/2009/10/29/…,但我不确定 gem 是否得到维护。
      猜你喜欢
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2012-01-12
      • 2012-10-28
      • 2020-11-06
      相关资源
      最近更新 更多