I can't send message in chat, with Microsoft Graph API "Message POST is allowed in application-only context only for import purposes"

Notificações 30 Reputation points
2023-02-06T19:12:29.9133333+00:00

I work at a company that asked me for an API that sends messages in a private chat in Teams

After my studies, I found the Microsoft Graph API and found out how to do it, in this link I can do it In this link I create the chat and send a message in the chat I created, from the id given to me when creating the chat

But when I do that, on my system it doesn't work

How I'm doing:

1- Create the Application

Here is my app permissions on azure User's image

2 - I create a chat, and get the id that this call returns

This is the code i make for do this, i take the id after this

var user_email = IT.DsEmail;
                var chat = new Chat
                {
                    ChatType = ChatType.OneOnOne,
                    Members = new ChatMembersCollectionPage()
                {
                    new AadUserConversationMember
                    {
                        Roles = new List<String>()
                        {
                            "owner"
                        },
                        AdditionalData = new Dictionary<string, object>()
                        {
                            {"user@odata.bind", "https://graph.microsoft.com/v1.0/users('notificacoes@vaxxinova.com.br')"}
                        }
                    },
                    new AadUserConversationMember
                    {
                        Roles = new List<String>()
                        {
                            "owner"
                        },
                        AdditionalData = new Dictionary<string, object>()
                        {
                            {"user@odata.bind", "https://graph.microsoft.com/v1.0/users('" + user_email + "')"}
                        }
                    }
                }
                };
var user = await graphServiceClient.Chats.Request().AddAsync(chat);

This works! I get the id with this call, and this id I would use to send the message

3 - Send a mesage in private chat

Now dont work! This is my code

var chatMessage = new ChatMessage
                {
                    Body = new ItemBody
                    {
                        Content = "Hello world"
                    }
                };
                
await graphServiceClient.Chats["19:35fa8078-de1a-4ec1-bb8f-85f1d4e14fa9_8848a24c-c679-415c-ac14-7f9dde86d6ec@unq.gbl.spaces"].Messages
                    .Request()
                    .AddAsync(chatMessage);

When i do that, i receive this error

Unauthorized\r\nMessage: Message POST is allowed in application-only context only for import purposes. Refer to https://docs.microsoft.com/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams for more details.

Can someone help me? Because in that link to test I can create the chat and send the message, but in the system I'm creating I can't? And why in my system I can create the chat, but I can't send the message?

I create the aplication, i create the chat, but i receive a error when i tried send a mensagem

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,067 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,582 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 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,465 questions
0 comments No comments
{count} votes

Accepted answer
  1. Siddharth Gautam 855 Reputation points
    2023-02-07T00:13:41.8366667+00:00

    Hello Notificações,

    Thanks for reaching out!

    As per the documentation, application permissions are supported only for migration purposes. For this, your team and channel both must be created in a migrated state. However, you can use this graph API in a delegated context (with signed-in user).

    Reference: Send ChatMessage

    Hope this helps.

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


1 additional answer

Sort by: Most helpful
  1. Rama Dayakar 0 Reputation points
    2023-10-02T14:55:15+00:00

    I had similar problem, Is it possible to send chat message with setting up migrate chat and as well as not sign in user. I have setup service which need to send message to team chat to get it approved. I have similar code as Notificações. Notificações if you find any solution please do share. Thank you in advance

    0 comments No comments