2

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.

Edijs Petersons
  • 537
  • 3
  • 6
  • 16

2 Answers2

1

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
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
  • Thanks, that's what I was looking for. I can add as well, that since it is ssl request, I had to change the code a little bit. `token = access_token['credentials']['token'] uri = URI.parse("https://graph.facebook.com/#{id}/friends?access_token=#{CGI.escape(token)}") #response = Net::HTTP.get_response(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` – Edijs Petersons Oct 10 '11 at 20:55
  • @EdijsPetersons, I also updated my answer to include your ssl changes – Ben Lee Oct 10 '11 at 21:32
0

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

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • I tried, but I got error when initializing `@graph = Koala::Facebook::API.new(access_token['credentials']['token'])`. access_token is coming after callback is made from facebook using omniauth devise environment variable `env["omniauth.auth"]`. I saw that from 1st October there were some object graph changes. Do you have any working example? – Edijs Petersons Oct 10 '11 at 20:50