Authorization process in Node.js is not working

Nofar 0 Reputation points
2024-04-28T08:48:07.5466667+00:00

Hello,

I want to connect to the Outlook Calendar API to read, create, update, and delete events. For this, I need to ask the user for consent.

The first thing I'm doing is to generate the login URL like this:

const msalConfig = {
  auth: {
    clientId: process.env.OUTLOOK_CLIENT_ID,
    authority: `https://login.microsoftonline.com/${process.env.OUTLOOK_TENANT_ID}`,
    redirectUri: process.env.REDIRECT_URL_DEVELOPMENT,
  },
};

const pca = new PublicClientApplication(msalConfig);

const ccaConfig = {
  auth: {
    clientId: process.env.OUTLOOK_CLIENT_ID,
    authority: `https://login.microsoftonline.com/${process.env.OUTLOOK_TENANT_ID}`,
    clientSecret: process.env.OUTLOOK_CLIENT_SECRET_VALUE,
  },
};

const cca = new ConfidentialClientApplication(ccaConfig);

const scopes = process.env.OUTLOOK_SCOPES || 'https://graph.microsoft.com/.default';
      const authCodeUrlParameters = {
        scopes: scopes.split(','),
        redirectUri: process.env.REDIRECT_URL_DEVELOPMENT,
      };

const url = await pca.getAuthCodeUrl(authCodeUrlParameters);
return url;

Then, I redirect the user to the URL link that I generated. After the user grants consent, I get a code to use to get the Access Token.

So this is the code I'm using to get the Access Token:

const scopes = process.env.OUTLOOK_SCOPES || 'https://graph.microsoft.com/.default';
const tokenRequest = {
  code: req.body.code,
  scopes: scopes.split(','),
  redirectUri: process.env.REDIRECT_URL_DEVELOPMENT,
};
let result;
try {
  result = await pca.acquireTokenByCode(tokenRequest);
  //accessToken
  console.log(result.accessToken);
} catch (e) {
  console.log(e.message);
}

So here I'm getting an error of: "invalid_client: The request body must contain the following parameter: 'client_assertion' or 'client_secret'."

Then, I tried to add the 'client_secret' to the tokenRequest, but I still got the same error.

Why am I getting this error and how can I fix it?

Hope someone can help!

Outlook | Windows | Classic Outlook for Windows | For business
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,371 Reputation points
    2024-04-29T03:09:08.89+00:00

    Hi @Nofar

    Try enabling public client flow for your app:

    9f809496-104d-466e-b0b5-606d65328e6f

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.