Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
using saveAuthToken fn to save returned response
setting yard ID as variable in the server so it can be saved with creds
  • Loading branch information
bmccann36 committed Feb 24, 2022
commit dc16996487a167a6bcaa41dd876f2b4720fce234
209 changes: 111 additions & 98 deletions sample/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

require('dotenv').config();

console.log(process.env.QB_CLIENT_ID)

/**
* Require the dependencies
* @type {*|createApplication}
*/
const express = require('express');
const TokenSaver = require('./TokenSaver')

const app = express();
const path = require('path');
const OAuthClient = require('intuit-oauth');
const bodyParser = require('body-parser');
const ngrok = process.env.NGROK_ENABLED === 'true' ? require('ngrok') : null;
const querystring = require("querystring");
const tokenSaver = new TokenSaver()

/**
* Configure View and Handlebars
*/
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, '/public')));
app.engine('html', require('ejs').renderFile);

app.set('view engine', 'html');
app.use(bodyParser.json());

const urlencodedParser = bodyParser.urlencoded({ extended: false });
const urlencodedParser = bodyParser.urlencoded({extended: false});

/**
* App Variables
Expand All @@ -41,152 +42,164 @@ let redirectUri = '';
*/

let oauthClient = null;
let yardId = null;

/**
* Home Route
*/
app.get('/', function (req, res) {
res.render('index');
res.render('index');
});

/**
* Get the AuthorizeUri
*/
app.get('/authUri', urlencodedParser, function (req, res) {
oauthClient = new OAuthClient({
clientId: process.env.QB_CLIENT_ID,
clientSecret: process.env.QB_CLIENT_SECRET,
environment: req.query.json.environment,
redirectUri: process.env.REDIRECT_URI,
});

const authUri = oauthClient.authorizeUri({
scope: [OAuthClient.scopes.Accounting, OAuthClient.scopes.Payment],
state: 'intuit-test',
});
res.send(authUri);


yardId = req.query.json.yardId
console.log('yardId is ', yardId)

oauthClient = new OAuthClient({
clientId: process.env.QB_CLIENT_ID,
clientSecret: process.env.QB_CLIENT_SECRET,
environment: req.query.json.environment,
redirectUri: process.env.REDIRECT_URI,
});

const authUri = oauthClient.authorizeUri({
scope: [OAuthClient.scopes.Accounting, OAuthClient.scopes.Payment],
state: 'intuit-test',
});
res.send(authUri);
});

/**
* Handle the callback to extract the `Auth Code` and exchange them for `Bearer-Tokens`
*/
app.get('/callback', function (req, res) {
oauthClient
.createToken(req.url)
.then(function (authResponse) {
console.log("auth response: *** \n", authResponse.token)
oauth2_token_json = JSON.stringify(authResponse.getJson(), null, 2);
})
.catch(function (e) {
console.error(e);
});

res.send('');
const parsedUrl = querystring.parse(req.url)
oauthClient
.createToken(req.url)
.then(function (authResponse) {
console.log("got auth response")
oauth2_token_json = JSON.stringify(authResponse.getJson(), null, 2);
return authResponse
})
.then(function (authResponse) {
return tokenSaver.saveAuthToken(authResponse.token, yardId)
})
.catch(function (e) {
console.error(e);
});

res.send('');
});

/**
* Display the token : CAUTION : JUST for sample purposes
*/
app.get('/retrieveToken', function (req, res) {
res.send(oauth2_token_json);
res.send(oauth2_token_json);
});

/**
* Refresh the access-token
*/
app.get('/refreshAccessToken', function (req, res) {
oauthClient
.refresh()
.then(function (authResponse) {
console.log(`The Refresh Token is ${JSON.stringify(authResponse.getJson())}`);
oauth2_token_json = JSON.stringify(authResponse.getJson(), null, 2);
res.send(oauth2_token_json);
})
.catch(function (e) {
console.error(e);
});
oauthClient
.refresh()
.then(function (authResponse) {
console.log(`The Refresh Token is ${JSON.stringify(authResponse.getJson())}`);
oauth2_token_json = JSON.stringify(authResponse.getJson(), null, 2);
res.send(oauth2_token_json);
})
.catch(function (e) {
console.error(e);
});
});

/**
* getCompanyInfo ()
*/
app.get('/getCompanyInfo', function (req, res) {
const companyID = oauthClient.getToken().realmId;

const url =
oauthClient.environment == 'sandbox'
? OAuthClient.environment.sandbox
: OAuthClient.environment.production;

oauthClient
.makeApiCall({ url: `${url}v3/company/${companyID}/companyinfo/${companyID}` })
.then(function (authResponse) {
console.log(`The response for API call is :${JSON.stringify(authResponse)}`);
res.send(JSON.parse(authResponse.text()));
})
.catch(function (e) {
console.error(e);
});
const companyID = oauthClient.getToken().realmId;

const url =
oauthClient.environment == 'sandbox'
? OAuthClient.environment.sandbox
: OAuthClient.environment.production;

oauthClient
.makeApiCall({url: `${url}v3/company/${companyID}/companyinfo/${companyID}`})
.then(function (authResponse) {
console.log(`The response for API call is :${JSON.stringify(authResponse)}`);
res.send(JSON.parse(authResponse.text()));
})
.catch(function (e) {
console.error(e);
});
});

/**
* disconnect ()
*/
app.get('/disconnect', function (req, res) {
console.log('The disconnect called ');
const authUri = oauthClient.authorizeUri({
scope: [OAuthClient.scopes.OpenId, OAuthClient.scopes.Email],
state: 'intuit-test',
});
res.redirect(authUri);
console.log('The disconnect called ');
const authUri = oauthClient.authorizeUri({
scope: [OAuthClient.scopes.OpenId, OAuthClient.scopes.Email],
state: 'intuit-test',
});
res.redirect(authUri);
});

/**
* Start server on HTTP (will use ngrok for HTTPS forwarding)
*/
const server = app.listen(process.env.PORT || 8000, () => {
console.log(`💻 Server listening on port ${server.address().port}`);
if (!ngrok) {
redirectUri = `${server.address().port}` + '/callback';
console.log(
`💳 Step 1 : Paste this URL in your browser : ` +
'http://localhost:' +
`${server.address().port}`,
);
console.log(
'💳 Step 2 : Copy and Paste the clientId and clientSecret from : https://developer.intuit.com',
);
console.log(
`💳 Step 3 : Copy Paste this callback URL into redirectURI :` +
'http://localhost:' +
`${server.address().port}` +
'/callback',
);
console.log(
`💻 Step 4 : Make Sure this redirect URI is also listed under the Redirect URIs on your app in : https://developer.intuit.com`,
);
}
console.log(`💻 Server listening on port ${server.address().port}`);
if (!ngrok) {
redirectUri = `${server.address().port}` + '/callback';
console.log(
`💳 Step 1 : Paste this URL in your browser : ` +
'http://localhost:' +
`${server.address().port}`,
);
console.log(
'💳 Step 2 : Copy and Paste the clientId and clientSecret from : https://developer.intuit.com',
);
console.log(
`💳 Step 3 : Copy Paste this callback URL into redirectURI :` +
'http://localhost:' +
`${server.address().port}` +
'/callback',
);
console.log(
`💻 Step 4 : Make Sure this redirect URI is also listed under the Redirect URIs on your app in : https://developer.intuit.com`,
);
}
});

/**
* Optional : If NGROK is enabled
*/
if (ngrok) {
console.log('NGROK Enabled');
ngrok
.connect({ addr: process.env.PORT || 8000 })
.then((url) => {
redirectUri = `${url}/callback`;
console.log(`💳 Step 1 : Paste this URL in your browser : ${url}`);
console.log(
'💳 Step 2 : Copy and Paste the clientId and clientSecret from : https://developer.intuit.com',
);
console.log(`💳 Step 3 : Copy Paste this callback URL into redirectURI : ${redirectUri}`);
console.log(
`💻 Step 4 : Make Sure this redirect URI is also listed under the Redirect URIs on your app in : https://developer.intuit.com`,
);
})
.catch(() => {
process.exit(1);
});
console.log('NGROK Enabled');
ngrok
.connect({addr: process.env.PORT || 8000})
.then((url) => {
redirectUri = `${url}/callback`;
console.log(`💳 Step 1 : Paste this URL in your browser : ${url}`);
console.log(
'💳 Step 2 : Copy and Paste the clientId and clientSecret from : https://developer.intuit.com',
);
console.log(`💳 Step 3 : Copy Paste this callback URL into redirectURI : ${redirectUri}`);
console.log(
`💻 Step 4 : Make Sure this redirect URI is also listed under the Redirect URIs on your app in : https://developer.intuit.com`,
);
})
.catch(() => {
process.exit(1);
});
}
Loading