azuremicrosoft-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"
            ],
            "user@odata.bind": "https://graph.microsoft.com/v1.0/users('{your-user-id}')"
        },
        {
            "@odata.type": "#microsoft.graph.aadUserConversationMember",
            "roles": [
                "owner"
            ],
            "user@odata.bind": "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

  • As mentioned in the MS Doc, sending messages with application permission are only supported for migration. So, it's not possible to send teams message with Application permissions.

    Initially, I too got same error when I tried to send message with token generated with client credentials flow:

    POST https://graph.microsoft.com/v1.0/chats/chatID/messages
    Content-type: application/json
    
    {
      "body": {
         "content": "Hello world"
      }
    }
    

    Response:

    enter image description here

    To resolve the error, switch to delegated flows like authorization code flow for generating access token.

    In my case, I added ChatMessage.Send permission of Delegated type in my application like this:

    enter image description here

    Now, I ran below authorization request in browser that gives code value in address bar after successful authentication:

    https://login.microsoftonline.com/tenantId/oauth2/v2.0/authorize
    ?client_id=appId
    &response_type=code
    &redirect_uri=https://jwt.ms
    &response_mode=query
    &scope=ChatMessage.Send
    &state=12345
    

    enter image description here

    You can use this code value to get access token using authorization code flow via Postman with below parameters:

    POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
    grant_type:authorization_code
    client_id:appId
    client_secret:secret
    scope:ChatMessage.Send
    code:code
    redirect_uri:https://jwt.ms
    

    Response:

    enter image description here

    When I used this token to send message in chat, I got the response successfully like this:

    POST https://graph.microsoft.com/v1.0/chats/chatID/messages
    Content-type: application/json
    
    {
      "body": {
         "content": "Hello world"
      }
    }
    

    Response:

    enter image description here