使用Net :: HTTP GET请求时出错

时间:2013-09-27 11:19:53

标签: ruby facebook-graph-api httprequest

我是红宝石的新手。我有以下代码片段执行GET操作并检索我的facebook组的名称

def get_groups
  query=("SELECT gid,name FROM group where gid in(SELECT gid from group_member where uid=me)")
  uri=URI("https://graph.facebook.com/fql")
  params={'q'=>query,'access_token'=>TOKEN}
  uri.query=URI.encode_www_form(params)
  response=Net::HTTP.get_response(uri)
  result=json.loads(response.text)
  return result['data']
end

但是当我执行上面的代码时,我得到以下错误:

/usr/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
    from /usr/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill'
    from /usr/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
    from /usr/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
    from /usr/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
    from /usr/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
    from /usr/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'
    from /usr/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
    from /usr/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
    from /usr/lib/ruby/1.9.1/net/http.rb:1293:in `request'
    from /usr/lib/ruby/1.9.1/net/http.rb:1195:in `request_get'
    from /usr/lib/ruby/1.9.1/net/http.rb:455:in `block in get_response'
    from /usr/lib/ruby/1.9.1/net/http.rb:745:in `start'
    from /usr/lib/ruby/1.9.1/net/http.rb:454:in `get_response'
    from fb.rb:12:in `get_groups'
    from fb.rb:32:in `<main>'

我在这里犯的错误是什么?

2 个答案:

答案 0 :(得分:2)

我认为这里的问题是您使用HTTP库来访问HTTPS服务。这些根本不同。这是一个HTTPS示例:

require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http.ssl_version = :TLSv1
http.ciphers = "ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:-LOW"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
req = Net::HTTP::Get.new('/', {'Content-Type' =>'application/json'})
http.start {|http| http.request(req) }

答案 1 :(得分:0)

您似乎有SSL证书验证问题。尝试关闭SSL验证。

Net::HTTP.verify_mode = OpenSSL::SSL::VERIFY_NONE

如果上述帮助您,问题的根源可能是当前的OpenSSL安装。尝试将其更新到最新版本。