Skip to content

Commit 196afa5

Browse files
committed
Add method for refreshing an access token
1 parent 6c88de9 commit 196afa5

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
@@ -786,6 +786,57 @@
786786
}
787787
};
788788

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

0 commit comments

Comments
 (0)