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

Add method for refreshing an access token #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,57 @@
}
};

/**
* @param clientId OAuth2 client ID: Identifies the client making the request.
* Client applications may be scoped to a limited set of system access.
* @param clientSecret the secret key you generated when you set up the integration in DocuSign Admin console.
* @param refreshToken The refresh token that you received from a previous <i>generateAccessToken</i> or <i>refreshAccessToken</i> callback.
* @return OAuthToken object.xx
*/
exports.prototype.refreshAccessToken = function(clientId, clientSecret, refreshToken, callback) {
if (!clientId) throw new Error('Error clientId is required', null);
if (!clientSecret) throw new Error('Error clientSecret is required', null);
if (!refreshToken) throw new Error('Error refreshToken is required', null);

var clientString = clientId + ":" + clientSecret,
postData = {
"grant_type": "refresh_token",
"refresh_token": refreshToken,
},
headers = {
"Authorization": "Basic " + (new Buffer(clientString).toString('base64')),
"Cache-Control": "no-store",
"Pragma": "no-cache"
},
OAuthToken = require('./OAuth').OAuthToken,
request = superagent.post("https://" + this.getOAuthBasePath() + "/oauth/token")
.send(postData)
.set(headers)
.type("application/x-www-form-urlencoded");

if (!callback) {
return new Promise(function (resolve, reject) {
request.end(function (err, res) {
if (err) {
reject(err);
} else {
resolve(OAuthToken.constructFromObject(res.body))
}
});
});
} else {
request.end(function (err, res) {
var OAuthToken;
if (err) {
return callback(err, res);
} else {
OAuthToken = require('./OAuth').OAuthToken;
return callback(err, OAuthToken.constructFromObject(res.body))
}
});
}
};

/**
* @param accessToken the bearer token to use to authenticate for this call.
* @return OAuth UserInfo model
Expand Down