'Problem to send message to teams channel with Microsoft graph

I'm working to send a message to Teams by using Graph API.

my application is a daemon application that sends a message automatically in the background.

I have written code like an official reference link below:

https://docs.microsoft.com/en-gb/graph/sdks/choose-authentication-providers?tabs=CS#client-credentials-provider

in my case, I use the client-credentials-provider but, I still can't send a message, and always get the below error message.

surely I have already registered my application in Azure and set for the grant of scope

enter image description here

https://i.stack.imgur.com/Dn8i0.png

How can I fix this?



Solution 1:[1]

Following this api document, you need to give Application api permission Teamwork.Migrate.All, and try this code below:

using Azure.Identity;
using Microsoft.Graph;

public void sendMesgAsync()
{

    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "your_tenant_name.onmicrosoft.com";
    var clientId = "azure_ad_app_client_id";
    var clientSecret = "client_secret";
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

    var chatMessage = new ChatMessage
    {
        Body = new ItemBody
        {
            Content = "Hello World"
        }
    };

    await graphClient.Teams["{team-id}"].Channels["{channel-id}"].Messages
        .Request()
        .AddAsync(chatMessage);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tiny Wang