2012-07-22 88 views
0

我嘗試使用Twitter API學習Ruby中的REST。使用Ruby獲取對Twitter API的請求,REST

根據https://dev.twitter.com/docs/api/1/get/trends我必須寫入GET請求到http://api.twitter.com/1/trends.json

我的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>' 

有什麼不對?

回答

0

你得到這個錯誤,因爲你試圖用http://api.twitter.com/1/trends.json獲取不存在trends docs

此方法已通過GET趨勢已被替換的資源,在這個文檔解釋/:WOEID 。 請使用新的端點更新您的應用程序。

您想獲取像這樣的網址https://api.twitter.com/1/trends/1.json。因此,在您的代碼中,請嘗試執行此操作:

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

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

puts response.body 

而且您應該得到響應。

+0

它的工作原理。非常感謝!我會仔細閱讀! – Danila 2012-07-22 10:47:32