【问题标题】:Get request to twitter API using Ruby, REST使用 Ruby、REST 获取对 twitter API 的请求
【发布时间】:2012-07-20 22:03:32
【问题描述】:

我尝试使用 Twitter API 在 Ruby 中学习 REST。

根据https://dev.twitter.com/docs/api/1/get/trends,我必须向http://api.twitter.com/1/trends.json写GET请求。

我的 Ruby 代码是:

 require 'rubygems'
 require 'rest-client'
 require 'json'

 url = 'http://api.twitter.com/1/trends.json'
 response = RestClient.get(url)

 puts response.body

但我遇到了下一个错误:

/home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient  /abstract_response.rb:48:in `return!': 404 Resource Not Found (RestClient::ResourceNotFound)

from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'

from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'


from /home/danik/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:745:in `start'

from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'

from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'

from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'

from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient.rb:68:in `get'

from TwitterTrends.rb:5:in `<main>'

怎么了?

【问题讨论】:

    标签: ruby json api rest twitter


    【解决方案1】:

    您收到该错误是因为您尝试使用 http://api.twitter.com/1/trends.json 获取的资源不存在,如本文档 trends docs 中所述

    此方法已被弃用,已被 GET Trends/:woeid 取代。 请使用新端点更新您的应用程序。

    您想获取像 https://api.twitter.com/1/trends/1.json 这样的 URL。所以,在你的代码中,尝试这样做:

     require 'rubygems'
     require 'rest-client'
     require 'json'
    
     url = 'https://api.twitter.com/1/trends/1.json'
     response = RestClient.get(url)
    
     puts response.body
    

    你应该得到回应。

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2015-06-11
      • 2015-04-03
      • 2021-04-25
      • 2018-10-10
      • 1970-01-01
      相关资源
      最近更新 更多