InvalidURIError making request to Facebook Graph API with Ruby -
i'm trying response api includes fields i'm specifying in uri string keep receiving invalidurierror. i've come here last resort, having spent hours trying debug this. i've tried using uri.encode() method on well, same error.
here's code:
url = params[:url] uri = uri('https://graph.facebook.com/v2.3/?id=' + url + '&fields=share,og_object{id,url,engagement}&access_token=' + config['fb_access_token']) req = net::http::post.new(uri.path) req.set_form_data('fields' => 'og_object[engagement]','access_token' => config['fb_access_token']) res = net::http.new(uri.host, uri.port) res.verify_mode = openssl::ssl::verify_none res.use_ssl = true response = nil res.start |http| response = http.request(req) end response = http.request(req) output = "" output << "#{response.body} <br />" return output
and error i'm receiving:
uri::invalidurierror - bad uri(is not uri?): https://graph.facebook.com/v2.3/?id=http://www.wikipedia.org&fields=share,og_object{id,url,engagement}&access_token=960606020650536|ejc0pocarfaqkzwzhdwn5ogkhfs
i'm exhausted @ point if left out important information let me know , i'll respond can. thank you!
the problem you're dumping strings uri without escaping them first.
since you're using sinatra can use rack::utils.build_query
construct uri's query component values correctly escaped:
uri = uri('https://graph.facebook.com/v2.3/') uri.query = rack::utils.build_query( id: url, fields: 'share,og_object{id,url,engagement}', access_token: config['fb_access_token'] )