繁体   English   中英

使用Ruby,REST获取对Twitter API的请求

[英]Get request to twitter API using Ruby, REST

我尝试使用Twitter API在Ruby中学习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>'

怎么了?

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

该方法已被弃用,并已由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

您应该得到回应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM