79234257

Date: 2024-11-28 14:24:58
Score: 0.5
Natty:
Report link

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.

  1. Stringify the body before sending to API call.
  2. You can directly call Microsoft Oauth token url using axios.
  3. If you are getting Self Signed in certificate chain error, you can temporarily disable the check using https agent.

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;
    }
}

Reasons:
  • Whitelisted phrase (-1): it worked
  • Whitelisted phrase (-1): worked for me
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): facing similar problem
  • User mentioned (1): @sridevi
  • Self-answer (0.5):
Posted by: Dr. Rahul Jha