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
Outlook
A family of Microsoft email and calendar products.
3,043 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,716 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,668 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 37,456 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.