Home Why is QuickBooks API not returning a refresh token?
Reply: 0

Why is QuickBooks API not returning a refresh token?

Andrew Dixon
1#
Andrew Dixon Published in 2018-01-12 23:41:33Z

I'm trying to setup a quickbooks integration with our rails app and I'm getting very weird results. So far, I've created an Active Admin page that lets me call out to Quickbooks' API to get an Oauth2 token:

  action_item :reset_token do
    session[:state] = SecureRandom.uuid
    quickbook_params = {
      response_type: 'code',
      state: session[:state],
      scope: 'com.intuit.quickbooks.accounting'
    }
    link_to "Reset Token", client.authorization_uri(quickbook_params)
  end

  page_action :oauth2_redirect do
    # test if the response has the state that we set to prevent a man-in-the-middle attack
    if session[:state] == params[:state]
      client.authorization_code = params[:code]
      resp = client.access_token!
      ENV["QBO_API_REALM_ID"] = params[:realmId]
      ENV["QBO_API_REFRESH_TOKEN"] = resp.refresh_token
      ENV["QBO_API_ACCESS_TOKEN"] = resp.access_token
    end
    redirect_to admin_quickbooks_path
  end

def client
  Rack::OAuth2::Client.new(
    identifier: ENV['QBO_API_IDENTIFIER'],
    secret: ENV['QBO_API_SECRET'],
    redirect_uri: Rails.application.routes.url_helpers.root_url + ENV['QBO_API_REDIRECT_URI'],
    authorization_endpoint: ENV["QBO_API_AUTHORIZATION_ENDPOINT"],
    token_endpoint: ENV["QBO_API_TOKEN_ENDPOINT"]
  )
end

The above "works" in that I get an access token (but no refresh token). the realm_id also matches the company id that I'm expecting so it seems to be recognizing the authorization attempt at least.

But, I'm not getting back a refresh_token and I keep getting 500s when I try to use the access_token to retrieve a customer:

$ curl -H "Authorization: bearer $auth_token" "https://sandbox-quickbooks.api.intuit.com/v3/company/$realm_id/customer/1"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><FaultInfo xmlns="http://www.intuit.com/sb/cdm/baseexceptionmodel/xsd"><Message>Internal Server Error</Message><ErrorCode>500</ErrorCode><Cause>SERVER</Cause></FaultInfo>

Which is the same error I see in my logs when I try to run a query from the rails app:

  action_item :run_query do
    link_to "Run Query", admin_quickbooks_query_path
  end

  page_action :query do
    QboApi.log = true # TODO: clean up so that we aren't always logging the Quickbooks API
    if ENV['QBO_API_ACCESS_TOKEN']
      qbo_api = QboApi.new(
        access_token: ENV['QBO_API_ACCESS_TOKEN'],
        realm_id: ENV['QBO_API_REALM_ID']
      )
      session[:qb_customer] = qbo_api.get :customer, 1 rescue "-- rescued error --"
    end
    redirect_to admin_quickbooks_path
  end

Any help would be greatly appreciated.

Thanks!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.269519 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO