SSL verification issue with the Foursquare2 gem [Solved]

I’ve been implementing a Foursquare API into a website using the foursqaure2 gem.

While working on development, everything went smooth, tests passed and it was really cool to work with. However, once I deployed staging and production I immediately encountered the well known SSL issues.

SSLv3 read server certificate B: certificate verify failed

The solution actually wasn’t that trivial, I monkeypatched the gem so you can pass an SSL hash and refer to the ca_file.

This has solved the problem.
Notice the @ssl param, this is the important thing here.

def initialize(options={})
      @client_id = options[:client_id]
      @client_secret = options[:client_secret]
      @oauth_token = options[:oauth_token]
      @ssl = options[:ssl].nil? ? Hash.new : options[:ssl]
    end

    # Sets up the connection to be used for all requests based on options passed during initialization.
    def connection
      params = {}
      params[:client_id] = @client_id if @client_id
      params[:client_secret] = @client_secret if @client_secret
      params[:oauth_token] = @oauth_token if @oauth_token
      @connection ||= Faraday::Connection.new(:url => api_url, :ssl => @ssl, :params => params, :headers => default_headers) do |builder|
        builder.adapter Faraday.default_adapter
        builder.use Faraday::Response::Mashify
        builder.use Faraday::Response::ParseJson
      end
    end

    def ssl
      @ssl
    end

This is how you pass the hash

    client = Foursquare2::Client.new(:oauth_token => self.fs_oauth_token, :ssl => { :verify => OpenSSL::SSL::VERIFY_PEER, :ca_file => "path_to_your_pem_file" })

If you are running on Amazon or any other cloud solution and you don’t know where your SSL certification is located, there’s a tip in this post on how you can find it.

Like a good Ruby on rails and open source developer, I issued a pull request to the original gem’s developer and this fix will be in the next version of the gem, you can find it here

If you are having issues with SSL, just pay attention to what version of the gem you are running and make sure you are running a version with this fix.