I have implemented Azure authentiation in web API created in angular 10 application. While authenticating using URL "https://login.microsoftonline.com/" I am getting Request failed with status 400 inside MSAL-NODE while calling networkClient.sendPostRequestAsync(tokenEndpoint, options). Here is detailed error I am getting and code snippet. I am using @azure/[email protected]
Code:
async callMSClient(): Promise<any> {
const msalConfig = {
auth: {
clientId: config.get("msConfig.clientId"),
authority: config.get("msConfig.aadEndPoint") + config.get("msConfig.tenantId"),
clientSecret: config.get("msConfig.clientSecret"),
redirectUri: config.get("msConfig.redirectUri"),
knownAuthorities: ["login.microsoftonline.com"]
}
};
const tokenRequest = {
scopes: [config.get("msConfig.graphEndPoint") + '.default'],
};
const cca = new msal.ConfidentialClientApplication(msalConfig);
return cca.acquireTokenByClientCredential(tokenRequest).then(data => {
return data;
}).catch(e=>{
console.log(e);
});
}
Error:
ClientAuthError: network_error: Network request failed
at createClientAuthError (C:ProjectAngTestnode_modules@azuremsal-commondisterrorClientAuthError.mjs:255:12)
at NetworkManager.sendPostRequest (C:ProjectAngTestnode_modules@azuremsal-commondistnetworkNetworkManager.mjs:35:23)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at ClientCredentialClient.executePostToTokenEndpoint (C:ProjectAngTestnode_modules@azuremsal-commondistclientBaseClient.mjs:79:26)
at ClientCredentialClient.executeTokenRequest (C:ProjectAngTestnode_modules@azuresrcclientClientCredentialClient.ts:295:30)
at ConfidentialClientApplication.acquireTokenByClientCredential (C:ProjectAngTestnode_modules@azuresrcclientConfidentialClientApplication.ts:166:20) {
errorCode: ‘network_error’,
errorMessage: ‘Network request failed’,
subError: ”,
correlationId: ‘ef035f69-5522-4224g-633s-ccddd950b2hg’
When I deep dive into error list, while debugging I found two error infact, one is error 400 stating bad request and another is "Self Singed In Certificate Chain" error which seems I have to install a certificate in my local machine for localhost.
2
Answers
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 agentconst 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.Initially, I registered one application and granted API permission of Application type as token generation uses client credentials flow:
Now, I ran below commands to create a project and installed dependencies in it:
In my case, I created following code files to generate the token with client credentials flow:
.env:
auth.js:
index.js:
When I started the server with
node index.js
command and ran below URL in browser, I got the token successfully as below:You can use this token to list users by calling below Microsoft Graph API call: