An error getting when i try to send a message to microsoft teams channel by using Graph API

Alwin James 1 Reputation point

Hi team,

I have created a console application in .NET core. I have installed Microsoft Graph into this application by using NuGet Packet manager. I got an error ("UnAuthorized") when I try to send a message to Microsoft teams channel . I mentioned by sample code below,

AzureGraphAuthConfig config = AzureGraphAuthConfig.ReadFromJsonFile("appsettings.json");
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
            graphClient = new GraphServiceClient(authProvider);

            var chatMessage = new ChatMessage
                Body = new ItemBody
                    Content = "Hello Team"

            var res = await graphClient.Teams[teamId].Channels[channelId].Messages.Request().AddAsync(chatMessage);

Getting an error When I execute this code. But working fine from Graph explorer.
Could you please suggest a proper solution for this?


Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,051 questions
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.
2,963 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sayali-MSFT 2,266 Reputation points Microsoft Vendor

    Make sure that your application is presenting a valid access token to Microsoft Graph as part of the request. This error often means that the access token may be missing in the HTTP authenticate request header or that the token is invalid or has expired. We strongly recommend that you use the Microsoft Authentication Library (MSAL) for access token acquisition. Additionally this error may occur, if you try to use a delegated access token granted to a personal Microsoft account, to access an API that only supports work or school accounts (organizational accounts).

    Reference document-

    0 comments No comments