Migrating a QuickBooks Online App from OAuth1 to OAuth2 using the qbo_api gem - Step 2
Show notes:
- Add gem 'rack-oauth2' to Gemfile
- Enable OAuth2 on your app at https://developer.intuit.com and set the redirect URI and grab the client_id and client_secret.
config/routes.rb
get '/oauth2-redirect', to: 'visitors#oauth2_redirect'
app/models/qbo/oauth2.rb
app/controllers/visitors_controller.rb
class VisitorsController < ApplicationController skip_before_action :authenticate_user!, :except => [:oauth2_redirect] def oauth2_redirect qbo_account = current_account.qbo_account if params[:state] == session[:oauth2_state] && qbo_account client = Qbo::OAuth2.client client.authorization_code = params[:code] if resp = client.access_token! attrs = { access_token: resp.access_token, refresh_token: resp.refresh_token, companyid: params[:realmId] }.merge(Qbo::OAuth2.expires_in) qbo_account.update!(attrs) msg = "Your QuickBooks account has been successfully linked." flash[:inner_notice] = msg render 'shared/close_and_redirect', layout: 'basic' end end rescue => e @url = account_url(current_account) msg = "There was a problem linking Your QuickBooks account - given an error: #{e.message}" logger.warn msg flash[:alert] = msg render 'shared/close_and_redirect', layout: 'basic' end end
app/controllers/application_controller.rb
def set_oauth2_state session[:oauth2_state] = Rails.env.test? ? '3242adf32423kjo' : SecureRandom.uuid end helper_method :set_oauth2_state
config/initializers/inflectors.rb
ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'OAuth2' end
view code
<% content_for :script do %> intuit.ipp.anywhere.setup({ grantUrl: '<%= Qbo::OAuth2.authorize_url(state: set_oauth2_state).html_safe %>', datasources: { quickbooks : true, payments : false } }); <% end %>
- Pushed on 11/06/2019 by Christian
- QuickBooks Integration Consulting