I was able to connect salesforce from postman and able to get access token, Issued_at parameter in response. But I am unable to see expires_in parameter. According to the OAuth 2.0 spec the expires_in
parameter is included with the Access Token response and provides the lifetime of the returned token in seconds.
Can someone help how do I get this parameter?
I have an API that doesn’t return the expiry in the response as its own key.
It is still contained in the token, but you will need to decode and extract it.
function parseJwt(token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
let decoded = parseJwt(token);
let expiryDate = new Date(decoded.exp * 1000);
I also use moment to format the expiry date and the current date time so they are in the same format for calculations.
let moment = require('moment');
let currentDateTime = moment(new Date()).format("YYYYMMDDHHmmss");
let tokenExpiry = moment(pm.environment.get("tokenExpiresOn")).format("YYYYMMDDHHmmss");
You can then do something like…
if (!pm.environment.get("JWT") || currentDateTime > tokenExpiry) {
// sendRequest to get new token.
}
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.