The answer is yes. You can either use the authorization helpers, or you can do this in a pre-request script.
You need to have the correct scope and grant type for your application.
You will call the Microsoft token endpoint directly to get a token for your application\grant.
The following is an example, that you can tailor to your requirements.
let moment = require('moment');
let currentDateTime = moment(new Date()).format("YYYYMMDDHHmmss");
let tokenExpiry = moment(pm.environment.get("bearerTokenExpiresOn")).format("YYYYMMDDHHmmss");
// console.log("currentDateTime: " + currentDateTime);
// console.log("tokenExpiry: " + tokenExpiry);
// console.log(pm.environment.get("bearerToken"));
if (!pm.environment.get("bearerToken") || currentDateTime > tokenExpiry) {
pm.test("Pre-request check for Environment Variables", function () {
let vars = ['client_id', 'scope', 'tenant_id', 'client_secret', 'scope'];
vars.forEach(function (item) {
// console.log(item);
pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.undefined;
pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.empty;
});
pm.sendRequest({
url: 'https://login.microsoftonline.com/' + pm.environment.get("tenant_id") + '/oauth2/v2.0/token',
method: 'POST',
header: 'Content-Type: application/x-www-form-urlencoded',
body: {
mode: 'urlencoded',
urlencoded: [
{ key: "client_id", value: pm.environment.get("client_id"), disabled: false },
{ key: "scope", value: pm.environment.get("scope"), disabled: false },
{ key: "client_secret", value: pm.environment.get("client_secret"), disabled: false },
{ key: "grant_type", value: "client_credentials", disabled: false },
]
}
}, function (err, res) {
if (err) {
console.log(err);
} else {
pm.test("Pre-request Microsoft login Status code is 200", () => {
pm.expect(res).to.have.status(200);
let resJson = res.json();
// console.log(resJson);
let token = resJson.access_token;
// console.log(token);
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);
// console.log(decoded);
let expiryDate = new Date(decoded.exp * 1000);
// console.log(expiryDate);
pm.environment.set("bearerToken", token);
pm.environment.set("bearerTokenExpiresOn", expiryDate);
// console.log("bearerTokenExpiresOn: " + pm.environment.get("bearerTokenExpiresOn"));
});
}
});
});
};
Pay attention to the expiry date, as I find that its different for some Microsoft endpoints. For some grant types you get an expiry date direct in the response, and in other situations like this one for client credentials, you have to pull apart the token. (The point Iām making is that you need to test this bit properly).
All you then need to use is use the bearerToken variable in the auth for your request.