How to get idtoken in MS teams

Ahmet Orkun Gedik 0 Reputation points
2025-04-19T15:48:06.53+00:00

Hi,

I am developing a chatbot on Teams framework. Regarding to customer request I need to call and external REST API on intranet and reply back to bot as an answer. To achieve this flow I am able to create accessToken through https://login.microsoftonline.com but it doesn't have a id token. To be able to get id token I am making another request to https://login.microsoftonline.com with Authorization code grant type. But it returns error below;

[onTurnError] unhandled error: InteractionRequiredAuthError: invalid_grant: AADSTS9002313: Invalid request. Request is malformed or invalid. Trace ID: a0da45bd-aba3-4626-bb8d-90eb1b7f0100 Correlation ID: bc6bd2fa-d520-4511-a912-b339239713b2 Timestamp: 2025-04-19 15:27:44Z

Please note that I am supplying access code that I created by acquireTokenByClientCredential method to acquireTokenByCode method. How can I get id token? Can anybody help me?

Please find the code below;

      const msalConfig = {
        auth: {
            clientId: "50254559-5530-40d1-a91b-4cd42e9dbb63",
            authority: "https://login.microsoftonline.com/20e81a44-49c5-46fe-bdbf-fd5d0b0342b1",
            clientSecret: "xxxxxxxxxxxx",
        },
        system: {
            loggerOptions: {
                loggerCallback(loglevel, message, containsPii) {
                    console.log(message);
                },
                piiLoggingEnabled: false,
                logLevel: 3,
            }
        }
      }

      var request = {
        scopes: [ 'https://graph.microsoft.com/.default' ]
      };
      
      var client = new msal.ConfidentialClientApplication(msalConfig);
      var response = await client.acquireTokenByClientCredential(request);
      const accessToken = response["accessToken"]

      var request1 = {
        code: accessToken,
        scopes: ["openid", "profile", "email"],
        redirectUri: "https://login.microsoftonline.com/20e81a44-49c5-46fe-bdbf-fd5d0b0342b1/oauth2/v2.0/authorize"
      };

      var response1 = await client.acquireTokenByCode(request1);

Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,847 questions
{count} votes

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.