Closed daytonlowell closed 4 years ago
Here's my example app using my wrapper:
const express = require('express')
const webServer = express()
const fs = require('fs')
const qbAuth = require('./auth.js')
const qbAuthSession = new qbAuth({
clientId: '<secret>',
clientSecret: '<also secret>',
storeToken: sessionData => {
return new Promise((resolve, reject) => {
fs.writeFile('./qb_auth_session.json', JSON.stringify(sessionData, null, 2), err => {
err ? reject(err) : resolve()
})
})
},
loadToken: () => {
return new Promise((resolve, reject) => {
fs.readFile('./qb_auth_session.json', 'utf8', (err, tokenData) => {
err ? reject(err) : resolve(JSON.parse(tokenData))
})
})
},
})
webServer.use(express.static('./static'))
webServer.get('/connect_to_quickbooks', qbAuthSession.connectToQuickbooks)
webServer.get('/callback', qbAuthSession.connectCallback)
webServer.get('/testApiCall', async(req, res) => {
const response = await qbAuthSession.makeApiCall({
body: `Select * from Account STARTPOSITION 1 MAXRESULTS 5\n`,
})
console.log(response)
res.send(response)
})
webServer.listen(3000, () => console.log('Example app listening on port 3000!'))
@daytonlowell : Went through your code. There are two places where the String to Object and Object to String conversion has caused errors in your code.
this.loadToken().then(token => {
console.log(`loaded token: `, token );
if (token) {
// setToken method accepts only JSON object
this.oauthClient.setToken(JSON.parse(token));
}
}).catch(err => {
console.error(err)
})
this.makeApiCall = async(req,res) => {
console.log(`makeApiCall getToken: `, this.oauthClient.token.getToken())
try {
// set the token first from qb_auth_session.json file
const token = await this.loadToken();
// parse the token
const parseToken = JSON.parse(token);
const response = await this.oauthClient.makeApiCall({
url: `https://sandbox-quickbooks.api.intuit.com/v3/company/${parseToken.realmId}/companyinfo/${parseToken.realmId}`
})
console.log("The response is : "+ JSON.stringify(response.getJson(),null,2));
} catch (err) {
console.error(err)
throw err
}
}
Thanks, I was double stringifying my JSON. 🤦
I wrote a wrapper that looks like this
The return of that
getToken()
call within mymakeApiCall
doesn't look valid. It looks likeThe token that got stored in my auth callback and later loaded and used in a
setToken
call, looks reasonable?Not sure what I'm doing wrong here.