Skip to content

Commit 9f76143

Browse files
committed
Add method for refreshing an access token
1 parent f4fee3f commit 9f76143

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/ApiClient.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,57 @@
782782
}
783783
};
784784

785+
/**
786+
* @param clientId OAuth2 client ID: Identifies the client making the request.
787+
* Client applications may be scoped to a limited set of system access.
788+
* @param clientSecret the secret key you generated when you set up the integration in DocuSign Admin console.
789+
* @param refreshToken The refresh token that you received from a previous <i>generateAccessToken</i> or <i>refreshAccessToken</i> callback.
790+
* @return OAuthToken object.xx
791+
*/
792+
exports.prototype.refreshAccessToken = function(clientId, clientSecret, refreshToken, callback) {
793+
if (!clientId) throw new Error('Error clientId is required', null);
794+
if (!clientSecret) throw new Error('Error clientSecret is required', null);
795+
if (!refreshToken) throw new Error('Error refreshToken is required', null);
796+
797+
var clientString = clientId + ":" + clientSecret,
798+
postData = {
799+
"grant_type": "refresh_token",
800+
"refresh_token": refreshToken,
801+
},
802+
headers = {
803+
"Authorization": "Basic " + (new Buffer(clientString).toString('base64')),
804+
"Cache-Control": "no-store",
805+
"Pragma": "no-cache"
806+
},
807+
OAuthToken = require('./OAuth').OAuthToken,
808+
request = superagent.post("https://" + this.getOAuthBasePath() + "/oauth/token")
809+
.send(postData)
810+
.set(headers)
811+
.type("application/x-www-form-urlencoded");
812+
813+
if (!callback) {
814+
return new Promise(function (resolve, reject) {
815+
request.end(function (err, res) {
816+
if (err) {
817+
reject(err);
818+
} else {
819+
resolve(OAuthToken.constructFromObject(res.body))
820+
}
821+
});
822+
});
823+
} else {
824+
request.end(function (err, res) {
825+
var OAuthToken;
826+
if (err) {
827+
return callback(err, res);
828+
} else {
829+
OAuthToken = require('./OAuth').OAuthToken;
830+
return callback(err, OAuthToken.constructFromObject(res.body))
831+
}
832+
});
833+
}
834+
};
835+
785836
/**
786837
* @param accessToken the bearer token to use to authenticate for this call.
787838
* @return OAuth UserInfo model

0 commit comments

Comments
 (0)