I'm working with the Intuit Developer API to fetch data from within a QuickBooks Sandbox account and I am encountering an unexpected error, and I don't know why.
The direct purpose of the API call is to fetch a list of customers from within the pre-written Sandbox data. The general purpose of the API call is so that invoices can be generated for one customer (the same customer) based on transactions completed within a smartphone application managed by that customer.
The response from the Intuit API is statusCode 401 which confirms a denial of authorisation, yet a fresh OAuth 2.0 access token is generated and supplied at the time of placing the API call.
My code for generating an OAuth 2.0 access token is as follows:
async function intuitAccess() { const tokenPATH = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'; const header = { 'Content-Type': 'application/x-www-form-urlencoded' }; const params = {'client_id': process.env.INTUIT_CLIENT_ID,'client_secret': process.env.INTUIT_CLIENT_SECRET,'scope': process.env.INTUIT_SCOPES,'grant_type': 'client_credentials' }; try { const tokenRequest = await correspond.post(tokenPATH).set(header).send(params); const responseData = tokenRequest.body; const token = responseData.access_token; console.log('Requested Token:', token); return token; } catch(error) { console.error('Error retrieving Intuit Access token:', error); } }
My code for requesting a list of customers is as follows:
async function intuitCustomerList() { try { const token = await intuitAccess(); if (!token) { console.log('IntuitCustomerList: Access token is null.'); return null; } // Delay for two seconds await new Promise(resolve => setTimeout(resolve, 2000)); const realmID = process.env.INTUIT_SANDBOX_REALM_ID; const query = 'SELECT * FROM Customer STARTPOSITION 1 MAXRESULTS 11'; const PATH = `https://sandbox-quickbooks.api.intuit.com/v3/company/${realmID}/query?query=${query}&minorversion=50`; const header = { "Authorization": 'Bearer '+ token, "Content-Type": "application/text" }; const response = await correspond.get(PATH).set(header); if (!response) { console.error('Intuit API Error: Response is null'); return null; } const responseData = response.text; console.log('Intuit Selected Response:', responseData); return responseData; } catch(error) { console.error('Error Obtaining Intuit Customer List:', error); return null; } }
Console logs confirm:
statusCode: 401, statusMessage: 'Unauthorized',
The question I have is, since what I have written results in an authorisation error, what is the correct way of manually writing the API call in NodeJS so that the data is fetched/returned as desired please?
For HTTP calls, that is superagent.
With thanks.