Facebook object graph https request from ruby

629 views Asked by At

How would you do a request to Facebook object graph to get the user's friends?

If you type in the url it works in the browser (replaced by valid user_id and access token): "https://graph.facebook.com/user_id/friends?access_token=2227470867|2.AQDi3TbqnqrsPa0_.360"

When I try it from ruby code using Net::HTTP.get_response(URI.parse('url')) I get URI::InvalidURIError error message.

2

There are 2 answers

2
Ben Lee On BEST ANSWER

Your access token has some characters that are invalid for a URL. You have to CGI.escape them.

require 'cgi'

access_token = '2227470867|2.AQDi3TbqnqrsPa0_.360'
url = "https://graph.facebook.com/user_id/friends?access_token=#{CGI.escape(access_token)}"
uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.path + "?" + uri.query)
response = http.request(request)
data = response.body
1
Bozhidar Batsov On

Maybe something to do with OAuth? I'd suggest you to use a library like Koala instead of unrolling custom adhoc solutions.