Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for OAuth2.0 using intuit-oauth library #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
86 changes: 86 additions & 0 deletions oauth2example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# node-quickbooks-oauth2.0-sample

Sample demonstrating Intuit's [QuickBooks API][1] using [intuit-oauth][2]

## Installation

```bash
$ npm install
$ npm start
```

## Documentation

1. You can authenticate and authorize using the instance of `intuit-oauth` library as shown in [app.js][3] :

```javascript

app.get('/requestToken', function (req, res) {

oauthClient = new OAuthClient({
clientId: config.clientId,
clientSecret: config.clientSecret,
environment: config.environment,
redirectUri: config.redirectUri
});

var authUri = oauthClient.authorizeUri({scope:[OAuthClient.scopes.Accounting],state:'node-quickbooks-oauth2-test'});
res.redirect(authUri);

});

```

2. You can make API calls by instantiating `Quickbooks` class as shown in [app.js][3] once you get the tokens from step 1 :

```javascript

app.get('/callback', function (req, res) {

var accessToken;

oauthClient.createToken(req.url)
.then(function(authResponse) {
accessToken = authResponse.getJson();
companyId = authResponse.token.realmId;
})
.then(function(response){
/**
* // save the access token somewhere on behalf of the logged in user
* @type {QuickBooks}
*/
var qbo = new QuickBooks(oauthClient.clientId,
oauthClient.clientSecret,
accessToken.access_token, /* oAuth access token */
false, /* no token secret for oAuth 2.0 */
companyId,
true, /* use a sandbox account */
true, /* turn debugging on */
34, /* minor version */
'2.0', /* oauth version */
accessToken.refresh_token /* refresh token */);

qbo.findAccounts(function (_, accounts) {
accounts.QueryResponse.Account.forEach(function (account) {
console.log(account.Name);
});
});
})
.catch(function(e) {
console.error(e);
});

res.send('<!DOCTYPE html><html lang="en"><head></head><body><script>window.opener.location.reload(); window.close();</script></body></html>');

});


```

**Note :** You can find the clientId and clientSecret for your app [here][4]


[1]: https://developer.intuit.com/docs/api/accounting
[2]: https://github.com/intuit/oauth-jsclient
[3]: https://github.com/mcohen01/node-quickbooks/blob/master/oauth2example/app.js
[4]: https://developer.intuit.com/app/developer/qbo/docs/get-started#create-an-app
103 changes: 51 additions & 52 deletions oauth2example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var app = express();
var QuickBooks = require('../index');
var Tokens = require('csrf');
var csrf = new Tokens();
var config = require('./config');
var OAuthClient = require('intuit-oauth');

QuickBooks.setOauthVersion('2.0');

Expand All @@ -28,10 +30,12 @@ app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});

// INSERT YOUR CONSUMER_KEY AND CONSUMER_SECRET HERE
/**
* Instantiate new Client
* @type {OAuthClient}
*/

var consumerKey = '';
var consumerSecret = '';
var oauthClient,companyId;

app.get('/', function (req, res) {
res.redirect('/start');
Expand All @@ -41,63 +45,58 @@ app.get('/start', function (req, res) {
res.render('intuit.ejs', { port: port, appCenter: QuickBooks.APP_CENTER_BASE });
});

// OAUTH 2 makes use of redirect requests
function generateAntiForgery (session) {
session.secret = csrf.secretSync();
return csrf.create(session.secret);
};

app.get('/requestToken', function (req, res) {
var redirecturl = QuickBooks.AUTHORIZATION_URL +
'?client_id=' + consumerKey +
'&redirect_uri=' + encodeURIComponent('http://localhost:' + port + '/callback/') + //Make sure this path matches entry in application dashboard
'&scope=com.intuit.quickbooks.accounting' +
'&response_type=code' +
'&state=' + generateAntiForgery(req.session);

res.redirect(redirecturl);

oauthClient = new OAuthClient({
clientId: config.clientId,
clientSecret: config.clientSecret,
environment: config.environment,
redirectUri: config.redirectUri
});

var authUri = oauthClient.authorizeUri({scope:[OAuthClient.scopes.Accounting],state:'node-quickbooks-oauth2-test'});
res.redirect(authUri);

});

app.get('/callback', function (req, res) {
var auth = (new Buffer(consumerKey + ':' + consumerSecret).toString('base64'));

var postBody = {
url: 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + auth,
},
form: {
grant_type: 'authorization_code',
code: req.query.code,
redirect_uri: 'http://localhost:' + port + '/callback/' //Make sure this path matches entry in application dashboard
}
};

request.post(postBody, function (e, r, data) {
var accessToken = JSON.parse(r.body);

// save the access token somewhere on behalf of the logged in user
var qbo = new QuickBooks(consumerKey,
consumerSecret,
accessToken.access_token, /* oAuth access token */
false, /* no token secret for oAuth 2.0 */
req.query.realmId,
true, /* use a sandbox account */
true, /* turn debugging on */
4, /* minor version */
'2.0', /* oauth version */
accessToken.refresh_token /* refresh token */);

qbo.findAccounts(function (_, accounts) {
accounts.QueryResponse.Account.forEach(function (account) {
console.log(account.Name);

var accessToken;

oauthClient.createToken(req.url)
.then(function(authResponse) {
accessToken = authResponse.getJson();
companyId = authResponse.token.realmId;
})
.then(function(response){
/**
* // save the access token somewhere on behalf of the logged in user
* @type {QuickBooks}
*/
var qbo = new QuickBooks(oauthClient.clientId,
oauthClient.clientSecret,
accessToken.access_token, /* oAuth access token */
false, /* no token secret for oAuth 2.0 */
companyId,
true, /* use a sandbox account */
true, /* turn debugging on */
34, /* minor version */
'2.0', /* oauth version */
accessToken.refresh_token /* refresh token */);

qbo.findAccounts(function (_, accounts) {
accounts.QueryResponse.Account.forEach(function (account) {
console.log(account.Name);
});
});
})
.catch(function(e) {
console.error(e);
});

});

res.send('<!DOCTYPE html><html lang="en"><head></head><body><script>window.opener.location.reload(); window.close();</script></body></html>');

});


6 changes: 6 additions & 0 deletions oauth2example/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
clientId: 'Enter the clientID',
clientSecret: 'Enter the clientSecret',
environment: 'Enter the environment', // 'sandbox' || 'production'
redirectUri: 'Enter the redirectUri'
}
6 changes: 5 additions & 1 deletion oauth2example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
"QuickBooks",
"Intuit Developer"
],
"scripts": {
"start": "node app"
},
"author": "Michael Cohen",
"license": "ISC",
"dependencies": {
"body-parser": "^1.13.3",
"cookie-parser": "^1.3.5",
"csrf": "^3.0.6",
"ejs": "2.5.5",
"express": "^4.13.3",
"express": "^4.17.0",
"express-session": "^1.11.3",
"intuit-oauth": "^1.2.0",
"node-quickbooks": "2.0.0"
}
}
Loading