IO-256 QBO Authorization Flow.
This commit is contained in:
@@ -6,26 +6,25 @@ require("dotenv").config({
|
||||
),
|
||||
});
|
||||
const OAuthClient = require("intuit-oauth");
|
||||
var Tokens = require("csrf");
|
||||
var tokens = new Tokens();
|
||||
|
||||
const oauthClient = new OAuthClient({
|
||||
clientId: process.env.QB_ONLINE_CLIENT_ID,
|
||||
clientSecret: process.env.QB_ONLINE_SECRET,
|
||||
environment: "sandbox", //process.env.NODE_ENV === "production" ? "production" : "sandbox",
|
||||
redirectUri: process.env.QB_ONLINE_REDIRECT_URI,
|
||||
clientId: process.env.QBO_CLIENT_ID,
|
||||
clientSecret: process.env.QBO_SECRET,
|
||||
environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
|
||||
redirectUri: process.env.QBO_REDIRECT_URI,
|
||||
});
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
console.log("QBO Authorize Called");
|
||||
const { userId } = req.body;
|
||||
console.log("exports.default -> userId", userId);
|
||||
// AuthorizationUri
|
||||
|
||||
const authUri = oauthClient.authorizeUri({
|
||||
scope: [OAuthClient.scopes.Accounting, OAuthClient.scopes.OpenId],
|
||||
state: tokens.create(userId),
|
||||
}); // can be an array of multiple scopes ex : {scope:[OAuthClient.scopes.Accounting,OAuthClient.scopes.OpenId]}
|
||||
console.log("authUri", authUri);
|
||||
// Redirect the authUri
|
||||
res.send(authUri);
|
||||
try {
|
||||
const authUri = oauthClient.authorizeUri({
|
||||
scope: [OAuthClient.scopes.Accounting, OAuthClient.scopes.OpenId],
|
||||
state: req.user.uid,
|
||||
}); // can be an array of multiple scopes ex : {scope:[OAuthClient.scopes.Accounting,OAuthClient.scopes.OpenId]}
|
||||
// Redirect the authUri
|
||||
res.send(authUri);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.sendStatus(500);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,31 +6,64 @@ require("dotenv").config({
|
||||
),
|
||||
});
|
||||
const OAuthClient = require("intuit-oauth");
|
||||
var Tokens = require("csrf");
|
||||
|
||||
var QuickBooks = require("node-quickbooks");
|
||||
const Promise = require("bluebird");
|
||||
const QuickBooksPromise = Promise.promisifyAll(QuickBooks.prototype);
|
||||
|
||||
const oauthClient = new OAuthClient({
|
||||
clientId: process.env.QB_ONLINE_CLIENT_ID,
|
||||
clientSecret: process.env.QB_ONLINE_SECRET,
|
||||
environment: "sandbox", //process.env.NODE_ENV === "production" ? "production" : "sandbox",
|
||||
redirectUri: process.env.QB_ONLINE_REDIRECT_URI,
|
||||
clientId: process.env.QBO_CLIENT_ID,
|
||||
clientSecret: process.env.QBO_SECRET,
|
||||
environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
|
||||
redirectUri: process.env.QBO_REDIRECT_URI,
|
||||
logging: true,
|
||||
});
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
// Parse the redirect URL for authCode and exchange them for tokens
|
||||
const parseRedirect = req.url;
|
||||
const { code, state, realmId } = req.query;
|
||||
console.log("exports.default -> state", state);
|
||||
// Exchange the auth code retrieved from the **req.url** on the redirectUri
|
||||
oauthClient
|
||||
.createToken(parseRedirect)
|
||||
.then(function (authResponse) {
|
||||
console.log("The Token is " + JSON.stringify(authResponse.getJson()));
|
||||
const { access_token, refresh_token } = authResponse.getJson();
|
||||
console.log("exports.default -> refresh_token", refresh_token);
|
||||
console.log("exports.default -> access_token", access_token);
|
||||
})
|
||||
.catch(function (e) {
|
||||
console.error("The error message is :" + e.originalMessage);
|
||||
console.error(e.intuit_tid);
|
||||
});
|
||||
|
||||
try {
|
||||
// Exchange the auth code retrieved from the **req.url** on the redirectUri
|
||||
const authResponse = await oauthClient.createToken(req.url);
|
||||
|
||||
const { access_token, refresh_token } = authResponse.json;
|
||||
|
||||
//store this information against the assocation record.
|
||||
|
||||
//Send a redirect back to the imex online application
|
||||
res.json(authResponse.json);
|
||||
|
||||
// var qbo = new QuickBooks(
|
||||
// process.env.QBO_CLIENT_ID,
|
||||
// process.env.QBO_SECRET,
|
||||
// access_token,
|
||||
// false, // no token secret for oAuth 2.0
|
||||
// realmId,
|
||||
// process.env.NODE_ENV !== "production", // use the sandbox?, // use the sandbox?
|
||||
// true, // enable debugging?
|
||||
// null, // set minorversion, or null for the latest version
|
||||
// "2.0", //oAuth version
|
||||
// refresh_token
|
||||
// );
|
||||
|
||||
// qbo.findInvoices({ fetchAll: true }, (errors, invoices) =>
|
||||
// console.log(errors, invoices)
|
||||
// );
|
||||
} catch (e) {
|
||||
console.error("The error message is :" + JSON.stringify(e, null, 2));
|
||||
console.error(e.intuit_tid);
|
||||
res.status(500).json(e);
|
||||
}
|
||||
};
|
||||
|
||||
exports.refresh = async (req, res) => {
|
||||
try {
|
||||
oauthClient.setToken(req.cookies.qbo_access_token);
|
||||
const authResponse = oauthClient.refreshUsingToken(
|
||||
req.cookies.qbo_refresh_token
|
||||
);
|
||||
res.json(authResponse.json);
|
||||
} catch (error) {
|
||||
res.status(500).json(error);
|
||||
}
|
||||
};
|
||||
|
||||
43
server/accounting/qbo/qbo-receivables.js
Normal file
43
server/accounting/qbo/qbo-receivables.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const path = require("path");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
const OAuthClient = require("intuit-oauth");
|
||||
|
||||
const oauthClient = new OAuthClient({
|
||||
clientId: process.env.QBO_CLIENT_ID,
|
||||
clientSecret: process.env.QBO_SECRET,
|
||||
environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
|
||||
redirectUri: process.env.QBO_REDIRECT_URI,
|
||||
});
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
try {
|
||||
oauthClient.setToken(req.cookies.qbo_access_token);
|
||||
|
||||
var qbo = new QuickBooks(
|
||||
process.env.QBO_CLIENT_ID,
|
||||
process.env.QBO_SECRET,
|
||||
req.cookies.qbo_access_token,
|
||||
false, // no token secret for oAuth 2.0
|
||||
req.cookies.qbo_realmId,
|
||||
process.env.NODE_ENV !== "production", // use the sandbox?, // use the sandbox?
|
||||
true, // enable debugging?
|
||||
null, // set minorversion, or null for the latest version
|
||||
"2.0", //oAuth version
|
||||
req.cookies.qbo_refresh_token
|
||||
);
|
||||
|
||||
qbo.findInvoices({ fetchAll: true }, (errors, invoices) =>
|
||||
console.log(errors, invoices)
|
||||
);
|
||||
|
||||
res.send({});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.sendStatus(500);
|
||||
}
|
||||
};
|
||||
@@ -1,5 +1,7 @@
|
||||
exports.callback = require("./qbo-callback").default;
|
||||
exports.authorize = require("./qbo-authorize").default;
|
||||
exports.refresh = require("./qbo-callback").refresh;
|
||||
exports.receivables = require("./qbo-receivables");
|
||||
|
||||
const OAuthClient = require("intuit-oauth");
|
||||
const path = require("path");
|
||||
|
||||
Reference in New Issue
Block a user