Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ruby to parse the json response given by foursquare API

I am using foursquare API to search for a particular venue. I use the following url: https://api.foursquare.com/v2/venues/venue_id?oauth_token=Access_token

The response given by the website is json.

Then I use the following code trying to parse this json response using ruby:

require 'rest-open-uri'
require 'json'

url = 'https://api.foursquare.com/v2/venues/venue_id?oauth_token=Access_token'

buffer = open(url, "UserAgent" => "Ruby-Wget").read

result = JSON.parse(buffer)

Here are the error code I get:

C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/net/http.rb:678:in connect': SSL_con nect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verif y failed (OpenSSL::SSL::SSLError) from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/net/http.rb:678:inbloc k in connect' from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/timeout.rb:44:in timeou t' from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/timeout.rb:89:intimeou t'

Does this error code mean that I cannot open this url because it starts with "https"? Is there any way that I could use to open this url and parse the json response?

Thanks ahead!

like image 337
user943413 Avatar asked Dec 13 '22 09:12

user943413


1 Answers

This should work:

gem install httparty

then

require 'httparty'
require 'json'

response = HTTParty.get("https://api.foursquare.com/v2/venues/vid", 
:query => {:oauth_token => "abc"})

json = JSON.parse(response.body)
like image 122
Matthew Rathbone Avatar answered Dec 30 '22 11:12

Matthew Rathbone