Search code examples
microsoft-graph-apiazure-ad-graph-api

Issue in sending personal message in MS Teams with Graph API


I registered my application in Azure AD website and collected the tenant ID, client ID, and client secret. I was able to generate the access token and chat ID for personal messaging which is requested in the below format

{
    "chatType": "oneOnOne",
    "members": [
        {
            "@odata.type": "#microsoft.graph.aadUserConversationMember",
            "roles": [
                "owner"
            ],
            "[email protected]": "https://graph.microsoft.com/v1.0/users('{your-user-id}')"
        },
        {
            "@odata.type": "#microsoft.graph.aadUserConversationMember",
            "roles": [
                "owner"
            ],
            "[email protected]": "https://graph.microsoft.com/v1.0/users('{user-id}')"
        }
    ]
}

I was able to generate chat ID for the mail. However when I tried the below end point

https://graph.microsoft.com/v1.0/chats/%7Bchat_id%7D/messages

for sending the message I am getting an error as below

{
    "error": {
        "code": "Unauthorized",
        "message": "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.",
        "innerError": {
            "date": "2024-06-10T05:06:10",
            "request-id": "4b1bf353-d1c0-4852-afcc-0c508d76d6d3",
            "client-request-id": "4b1bf353-d1c0-4852-afcc-0c508d76d6d3"
        }
    }
}

Why am I getting this error?

I am expecting the token generated from application registration credentials to send personal messages in teams. However I am facing this issue. Now I am able to generate chat ID with any two user mail IDs in my organization. But I am blocked from sending personal Teams messages.


Solution

  • Microsoft Graph API - Is it possible send chat message using client credentials flow?

    These were the scope permissions given by me.

    ChannelMessage.Edit ChannelMessage.Read.All ChannelMessage.ReadWrite ChannelMessage.Send Chat.Create Chat.ManageDeletion.All Chat.Read Chat.ReadBasic Chat.ReadWrite Chat.ReadWrite.All ChatMember.Read ChatMember.ReadWrite ChatMessage.Read ChatMessage.Send User.Read profile openid email. Attaching screenshot for reference.

    enter image description here

    I used this approach and it worked for me.. Thank you Sreedevi for posting this in the forum.