Ruby:使用Net :: HTTP到Facebook API的POST批处理请求的语法

时间:2012-12-31 21:53:48

标签: ruby facebook-graph-api

使用curl,以下命令是正确的,Facebook API会回复预期的响应:

  

curl -F'access_token = TOKEN'-F'batch = [{“method”:“GET”,   “relative_url”: “脸谱”},{ “方法”: “GET”,   “relative_url”:“youtube”}]'https://graph.facebook.com

我想将其转换为Ruby,但一直在努力寻找合适的语法。我在下面尝试了各种变化,但没有运气:

  

uri = URI.parse('https://graph.facebook.com')

     

res = Net :: HTTP.post_form(uri,'access_token'=>'TOKEN','batch'=>   “[{'method':'GET','relative_url':'facebook'}]”)

你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

感谢this questionthis blog post这就是我的工作:

uri = URI("https://graph.facebook.com/")
req = Net::HTTP::Post.new(uri.path)
attach = {}

attach = {'batch' => [{"method" => "GET", "relative_url"=>"facebook"}].to_json}
req.set_form_data(attach.merge('access_token' => "TOKEN"))

res = Net::HTTP.new(uri.host, uri.port)
res.verify_mode = OpenSSL::SSL::VERIFY_NONE
res.use_ssl = true

response = nil
res.start do |http|
  response = http.request(req)
end