尝试使用HttpParty(ruby)从twitter获取好友列表
我正在尝试从twitter获取特定用户的好友列表。尝试使用HttpParty(ruby)从twitter获取好友列表,ruby,twitter,httparty,Ruby,Twitter,Httparty,我正在尝试从twitter获取特定用户的好友列表。 这是我的代码- require 'rubygems' require 'httparty' class TwitterData include HTTParty base_uri 'http://api.twitter.com/1/' default_params :output => 'json' format :json def self.get_username_data(username) get(
这是我的代码-
require 'rubygems'
require 'httparty'
class TwitterData
include HTTParty
base_uri 'http://api.twitter.com/1/'
default_params :output => 'json'
format :json
def self.get_username_data(username)
get('statuses/friends.json' , :query => { :screen_name => username })
end
end
puts "Please your twitter username - "
twitter_username = gets
puts TwitterData.get_username_data(twitter_username).inspect
这就是我得到的错误-
Please your twitter username -
twitter
C:/Ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:14:in `rescue in parse': Invalid JSON string (Crack::ParseError)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:12:in `parse'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:116:in `json'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:136:in `parse_supported_format'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:103:in `parse'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:66:in `call'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:180:in `parse_response'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:164:in `handle_response'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:57:in `perform'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:280:in `perform_request'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:232:in `get'
from twitter_friends_2.rb:11:in `get_username_data'
from twitter_friends_2.rb:17:in `<main>'
请输入您的twitter用户名-
啁啾
C:/Ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:14:in'rescue in parse':无效的json字符串(crack::ParseError)
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:12:in'parse'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:116:in`json'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:136:parse\u-supported\u格式
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:103:in'parse'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:66:in'call'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:180:in'parse_response'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:164:in'handle_response'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:57:in'perform'
来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:280:“执行请求”中
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:232:in'get'
来自twitter\u friends\u 2.rb:11:in'get\u username\u data'
来自twitter\u friends\u 2.rb:17:in`'
将您的方法替换为此
def self.get_username_data(username)
get("/statuses/friends.json?screen_name=#{username}")
end
或
您需要删除用户名,因为当用户输入twitter用户名并点击enter键时,会在用户名后面追加“\n”,并将相同的用户名作为导致问题的参数发送。上面给出的任何代码片段都应该可以使用。您是否尝试了用户id而不是屏幕名称?这似乎是Crack的问题。我在使用宝石时偶尔会遇到:/
def self.get_username_data(username)
get('/statuses/friends.json' , :query => { :screen_name => username.strip })
end