尝试使用 httparty(ruby) 从 Twitter 获取好友列表

发布于 2024-10-05 15:13:19 字数 1873 浏览 3 评论 0原文

我正在尝试从 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('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>'

I am trying to get the list of specific user`s friends from twitter.
This is my code -

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

This is the error I am getting -

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-10-12 15:13:19

用此替换您的方法

def self.get_username_data(username)
  get("/statuses/friends.json?screen_name=#{username}")
end

或者

def self.get_username_data(username)
  get('/statuses/friends.json' , :query => { :screen_name => username.strip })
end

您需要删除用户名,因为当用户从命令行输入 twitter 用户名并按回车键时,“\n”会附加到用户名上,并且相同的用户名作为导致问题的参数发送。上面给出的任何代码片段都应该可以工作。

Replace your method with this

def self.get_username_data(username)
  get("/statuses/friends.json?screen_name=#{username}")
end

OR

def self.get_username_data(username)
  get('/statuses/friends.json' , :query => { :screen_name => username.strip })
end

You need to strip the username because from command line when user enters the twitter username and hits enter key a "\n" gets appended to the username and the same username is sent as a parameter which causes the problem. Any of the above given code snippets should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文