Deconstructing the qbo_api gem

I recently built a CSV parser and Quickbooks importer using the qbo_api gem.  The documentation for the gem was not easy for me to understand, but I was able to hack the functionality I needed into the sample Sinatra app that’s provided with the gem.

I now want to create another Quickbooks import tool and use it with a rails app that I’ve already built.  The trouble is figuring out how to set up a rails app to use the qbo_api gem.  There are no explicit instructions on how to do this in the gem’s documentation.

After a few hours of hacking around last night with a buddy, I have a slightly better understanding of what’s going on under the hood with the gem.

Looking at the example app provided with the gem, you can see from the require statements that there are a few other gems required:  omniauth and omniauth-quickbooks.  Omniauth-quickbooks is a strategy for omniauth to connect to quickbooks, specifically.

Lines 19-21 of the example app tell Sinatra to use the omniauth-quickbooks strategy, with tokens that are stored in the .env file.  Lines 31 – 36 set up the index page of the app, where you connect to quickbooks:

qbo-apiQboApi::APP_CENTER_BASE is a url where some Quickbooks JavaScript is hosted:  ““.  This is used in a script src tag on index.erb which loads the Quickbooks javascript library for connecting the api.  oauth_data is a method that returns a hash with oauth tokens.


In the hash, consumer_key and consumer_secret are constants, pulled from the .env file when the app loads.  But the token, token_secret, and realm_id are pulled from the session.  When the index page is first loaded, these haven’t been saved to the session yet.

The assignment to the session happens in the last route, starting at line 64:

get authThis method is called from index.erb, which is mainly two javascripts, explained here.

I haven’t yet figured out what env[“omniauth.auth”][:credentials] are, but I assume it is created by the omniauth gem and holds the tokens returned from Quickbooks when you connect.

Leave a Reply

Your email address will not be published. Required fields are marked *