Skip to content
Merged
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,23 @@ Follow the instructions below to use the library :
clientId: '<Enter your clientId>',
clientSecret: '<Enter your clientSecret>',
environment: 'sandbox' || 'production',
redirectUri: '<Enter your callback URL>'
redirectUri: '<Enter your callback URL>',
autoRefresh: true,
autoRefreshIntervalInSeconds: 55 * 60
});
```

### Options

* `clientId` - clientID for your app. Required
* `clientSecret` - clientSecret fpor your app. Required
* `clientSecret` - clientSecret for your app. Required
* `environment` - environment for the client. Required
* `sandbox` - for authorizing in sandbox.
* `production` - for authorizing in production.
* `redirectUri` - redirectUri on your app to get the `authorizationCode` from Intuit Servers. Required
* `logging` - by default, logging is disabled i.e `false`. To enable provide`true`.
* `redirectUri` - redirectUri on your app to get the `authorizationCode` from Intuit Servers. Required
* `autoRefresh` - by default, autoRefresh is disabled i.e `false`. To enable provide `true`. If set to true, OAuthClient would refresh the tokens every `autoRefreshIntervalInSeconds` specified so clients do not have to manually call refresh after the first successful `createToken` call. Optional.
* `autoRefreshIntervalInSeconds` - only if `autoRefresh` is set to true, config will use `autoRefreshIntervalInSeconds`. It is set in seconds. If user does not specify a value but has set `autoRefresh` as true, it would default to 55 minutes. Use `stopAutoRefresh` method on OAuthClient anytime to stop auto Refresh. Optional.
* `logging` - by default, logging is disabled i.e `false`. To enable provide`true`. Optional.



Expand Down Expand Up @@ -557,4 +561,4 @@ See the changelog [here](https://github.com/intuit/oauth-jsclient/blob/master/CH

Intuit `oauth-jsclient` is licensed under the [Apache License, Version 2.0](https://github.com/intuit/oauth-jsclient/blob/master/LICENSE)

[ss1]: https://help.developer.intuit.com/s/SDKFeedback?cid=1120
[ss1]: https://help.developer.intuit.com/s/SDKFeedback?cid=1120
13 changes: 12 additions & 1 deletion sample/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ app.get('/authUri', urlencodedParser, function(req,res) {
clientId: req.query.json.clientId,
clientSecret: req.query.json.clientSecret,
environment: req.query.json.environment,
redirectUri: req.query.json.redirectUri
redirectUri: req.query.json.redirectUri,
autoRefresh: req.query.json.autoRefresh,
autoRefreshIntervalInSeconds: 30,
logging: true
});

const authUri = oauthClient.authorizeUri({scope:[OAuthClient.scopes.Accounting],state:'intuit-test'});
Expand Down Expand Up @@ -91,6 +94,14 @@ app.get('/retrieveToken', function(req, res) {
});


/**
* Stop auto refresh of tokens if `autoRefresh` is set to true in config while creating the OAuthClient
*/
app.get('/stopAutoRefresh', function(req, res) {
oauthClient.stopAutoRefresh();
res.send('');
});

/**
* Refresh the access-token
*/
Expand Down
24 changes: 21 additions & 3 deletions sample/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ <h2>OAuth2.0</h2><h4>( Please enter the client credentials below )</h4><br>
<form>
<div class="form-group">
<label for="clientId">ClientID</label>
<input type="text" class="form-control" placeholder="enter the clientId" id="clientId" />
<input type="text" class="form-control" placeholder="enter the clientId" id="clientId"/>
</div>
<div class="form-group">
<label for="clientSecret">ClientSecret</label>
<input type="text" class="form-control" placeholder="enter the clientSecret" id="clientSecret" />
<input type="text" class="form-control" placeholder="enter the clientSecret" id="clientSecret"/>
</div>
<div class="form-group">
<label for="autoRefresh">AutoRefresh</label>
<input type="checkbox" id="autoRefresh" name="autoRefreshSelected" checked/>
</div>
<div class="form-group">
<label for="environment">Environment</label>
Expand All @@ -43,14 +47,15 @@ <h2>OAuth2.0</h2><h4>( Please enter the client credentials below )</h4><br>
</div>
<div class="form-group">
<label for="redirectUri">Redirect URI</label>
<input type="text" class="form-control" placeholder="enter the redirectUri" id="redirectUri" /><br>
<input type="text" class="form-control" placeholder="enter the redirectUri" id="redirectUri"/><br>
</div>
</form>
<p>Now click the <b>Connect to QuickBooks</b> button below.</p>
<pre id="accessToken"></pre>
<a class="imgLink" href="#" id="authorizeUri" ><img src="./images/C2QB_green_btn_lg_default.png" width="178" /></a>
<button type="button" id="retrieveToken" class="btn btn-success">Display Access Token</button>
<button type="button" id="refreshToken" class="btn btn-success">Refresh Token</button>
<button type="button" id="stopAutoRefresh" class="btn btn-success">Stop AutoRefresh</button>
<hr />

<h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="https://developer.intuit.com/v2/apiexplorer?apiname=V3QBO#?id=Account">API Explorer</a> )</h4>
Expand Down Expand Up @@ -84,6 +89,7 @@ <h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="http
jsonBody.clientSecret = $('#clientSecret').val();
jsonBody.environment = $('#environment').val();
jsonBody.redirectUri = $('#redirectUri').val();
jsonBody.autoRefresh = $('#autoRefresh').is(':checked')

$.get('/authUri', {json:jsonBody}, function (uri) {
console.log('The Auth Uris is :'+uri);
Expand Down Expand Up @@ -116,6 +122,13 @@ <h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="http
});
}

function stopAutoRefresh() {
// Stop AutoRefresh of the token
$.get('/stopAutoRefresh', function () {
$("#stopAutoRefresh").html();
});
}

function refreshToken() {

// Generate the authUri
Expand Down Expand Up @@ -143,6 +156,11 @@ <h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="http
retrieveToken();
});

document.getElementById('stopAutoRefresh').addEventListener('click', function response(e) {
e.preventDefault();
stopAutoRefresh();
});

document.getElementById('refreshToken').addEventListener('click', function response(e) {
e.preventDefault();
refreshToken();
Expand Down
19 changes: 19 additions & 0 deletions src/OAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function OAuthClient(config) {
this.clientId = config.clientId;
this.clientSecret = config.clientSecret;
this.redirectUri = config.redirectUri;
this.autoRefresh = config.autoRefresh || false;
// If autoRefreshIntervalInSeconds is not choosen by user, defaults to 3300 seconds (55 minutes) since tokens expire after 60 minutes.
this.autoRefreshIntervalInSeconds = config.autoRefreshIntervalInSeconds || 3300;
this.token = new Token(config.token);
this.logging = !!(Object.prototype.hasOwnProperty.call(config, 'logging') && config.logging === true);
this.logger = null;
Expand Down Expand Up @@ -160,6 +163,11 @@ OAuthClient.prototype.createToken = function createToken(uri) {
const authResponse = res.json ? res : null;
const json = (authResponse && authResponse.getJson()) || res;
this.token.setToken(json);
if (this.autoRefresh) {
this.log('info', 'Setting AutoRefresh of tokens every: ', this.autoRefreshIntervalInSeconds + ' seconds');
// Set auto refresh call
this.autoRefreshHandle = setInterval(() => this.refresh(), this.autoRefreshIntervalInSeconds * 1000);
}
this.log('info', 'Create Token response is : ', JSON.stringify(authResponse, null, 2));
return authResponse;
}).catch((e) => {
Expand Down Expand Up @@ -650,6 +658,17 @@ OAuthClient.prototype.setToken = function setToken(params) {
return this.token;
};

/**
* Stops auto refresh of tokens
*/
OAuthClient.prototype.stopAutoRefresh = function () {
if (this.autoRefreshHandle) {
clearInterval(this.autoRefreshHandle);
} else {
this.log('info', 'AutoRefresh is not set so it cannot be cleared');
}
};


/**
* Get AuthHeader
Expand Down
Loading