def refresh_stored_tokens(): # refresh stored QBO tokens callback_url = app.config.get('CALLBACK_URL') realm_id = fetch('realm_id') session_manager = Oauth2SessionManager( client_id=fetch('qb_id'), client_secret=fetch('qb_secret'), base_url=callback_url, access_token=fetch('access_token'), refresh_token=fetch('refresh_token'), ) result = session_manager.refresh_access_tokens(return_result=True) save('access_token', session_manager.access_token) save('refresh_token', session_manager.refresh_token) session_manager = Oauth2SessionManager( client_id=realm_id, client_secret=fetch('qb_secret'), access_token=fetch('access_token'), ) sandbox = fetch('qb_sandbox') qbclient = QuickBooks(sandbox=sandbox, session_manager=session_manager, company_id=realm_id) QuickBooks.enable_global() save('session_manager', session_manager) save('qbclient', qbclient) return str(result)
def get_client(realm, refresh_token): ''' Create the QuickBooks client object from the auth client. ''' QuickBooks.enable_global() qb = QuickBooks(auth_client=current_app.quickbooks_auth_client, refresh_token=refresh_token, company_id=realm) return qb
def refresh_session(): s = json.loads(get_secret()) auth_client = AuthClient( client_id=s["client_id"], client_secret=s["client_secret"], redirect_uri=s["redirect_url"], access_token=s["access_token"], refresh_token=s["refresh_token"], environment="production", ) # caution! invalid requests return {"error":"invalid_grant"} quietly auth_client.refresh() s["access_token"] = auth_client.access_token s["refresh_token"] = auth_client.refresh_token put_secret(json.dumps(s)) QuickBooks.enable_global() QuickBooks(auth_client=auth_client, company_id="1401432085") return auth_client
def set_global_vars(realmid, code): # stores Intuit tokens # stores QBO client object # stores session manager object for future token refreshes callback_url = app.config.get('CALLBACK_URL') session_manager = Oauth2SessionManager( client_id=fetch('qb_id'), client_secret=fetch('qb_secret'), base_url=callback_url, ) realm_id = realmid data = session_manager.get_access_tokens( auth_code=code, return_result=True, ) # sanity check: if no valid response from Intuit, abort fn if 'token_type' not in data: return None access_token = session_manager.access_token refresh_token = session_manager.refresh_token session_manager = Oauth2SessionManager( client_id=realm_id, client_secret=fetch('qb_secret'), access_token=access_token, ) sandbox = fetch('qb_sandbox') qbclient = QuickBooks(sandbox=sandbox, session_manager=session_manager, company_id=realm_id) QuickBooks.enable_global() save('realm_id', realm_id) save('access_token', access_token) save('refresh_token', refresh_token) save('session_manager', session_manager) save('qbclient', qbclient) from app import tasks