Go back to the main page

Access the QuickBooks Online API with OAuth2

 

OAuth2 required for new Intuit Developer Accounts

Starting July 17th, 2017 all new Intuit Developer accounts will need to use OAuth2 for API access. Here is how to use the QboApi gem and OAuth2.

OAuth2 for new Intuit Developer Accounts.

Choosing an OAuth2 gem or not

I really won't bother trying to re-invent the wheel by rolling your own OAuth2 code. That said, the OAuth2 2-legged process is simpler than the 3-legged OAuth 1a process and if you do want to roll your own OAuth2 code take a look at Intuit's Python example for a good starting point.

As for me, I'll choose to leverage the Rack-OAuth2 gem as I like its ability to directly set endpoints.

Spinning up an OAuth2 example

  1. Clone the qbo_api gem, switch into the new directory, and bundle
  2. $ git clone git://github.com/minimul/qbo_api && cd qbo_api
    $ bundle
    
  3. Create a .env file with the client_id and client_secret provided within the App settings page. See Fig. 2.
  4. export QBO_API_CLIENT_ID=
    export QBO_API_CLIENT_SECRET=
    
    Make a new app, I called this one QboApi OAuth2 Inc then go into its settings and then click on the Keys tab to get the client id and client secret. Put those values in the .env file.
    This section is at the 2:16 mark.
  5. Set the OAuth2 callback or redirect URI to http://localhost:9393/oauth2-redirect
  6. Massively IMPORTANT step
  7. Start up the example app
  8.   $ shotgun example/app.rb
    
    Firing up the QboApi sample app.
  9. Goto https://localhost:9393/oauth2
  10. Make sure to use localhost and not 127.0.0.1. Intuit only allows the host name localhost for OAuth2 sandbox callbacks.
    As you can see in the example/app.rb file the callback URI is also properly set to localhost and not 127.0.0.1.
    This section is at the 5:19 mark.
  11. Click on the 'Connect To QuickBooks' button, Sign in, and click on Authorize.
  12. If you are not already signed in, you are going to sign in with your Intuit Developer credentials in case you might be confused. In short you are signing in and connecting your QuickBooks app's sandbox to your QuickBooks app's "App". Clear as mud? Lastly, click "Authorize".
    This section is at the 8:51 mark.
  13. The response.
  14. Here is a successful response back. Highlighted is the access token.
  15. The response code.
  16. Check out what's going on under the hood on this response.
  17. Click on Click here to make an API call.
  18. Let's test the new access token.
    Success. We retrieved Sandbox customer # 5 ‐ "Dukes Basketball Camp".
    Reference the example/app.rb to see how to make a basic QuickBooks API call with the QBO API gem.