Here is the alternate for the problem I applied to solution.I acknowldge that @sridevi solution should work mostly but I had another problem also which I found only after deep dive. Posting if someone else is facing similar problem.
Below code worked for me.
I used grant_type:"client_credentials"
and added custom agent const agent=new https.Agent({rejectUnauthorized:false})
to disable Self Signed in Certificate chain error. This is not recommended but for testing on localhost it worked for me. In higher environment you should add verified certificate from CA.
async callApi(accessToken) {
const agent=new https.Agent({rejectUnauthorized:false});
const url= "https://login.microsoftonline.com/{{id}}/oauth2/v2.0/token";
const body = new URLSearchParams({
client_id: config.get("msConfig.clientId"),
grant_type:"client_credentials",
client_secret: config.get("msConfig.clientSecret"),
scope:"https://graph.microsoft.com/.default"
})
try {
const response = await axios.post (url, body, {
responseType:"json",
headers:{
'Content-Type': 'application/x-www-form-urlencoded'
},httpsAgent: agent});
return {accessToken: response.data.access_token,
status: response.status
};
} catch (error) {
console.log(error)
return error;
}
}