Search code examples
node.jsazurebotframeworkbotsmicrosoft-teams

MSTEAMS BOT - post message to personalChat using rest api - Authorization has been denied for this request


My objective is to post a message on personalChat in msteamsbot using restapi endpoint.

auth token is generated using below logic (reference doc)

    let url = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token";
    const data = { 
        'grant_type': 'client_credentials',
        'client_id' : '...',
        'client_secret' : '...',
        'scope' : 'https://api.botframework.com/.default'
    };
    const options = {
      method: 'POST',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      data: qs.stringify(data),
      url,
    };
    axios(options);

the token generated using above logic is passed to below api (reference doc). This api is supposed to post a message on the recipient's chat. but this doesn't happen.

curl --location --request POST 'https://smba.trafficmanager.net/apis/v3/conversations/a:1NYEIhjRpL_RgDBDaA7lJyO6kl8rbpWQLyKkayQnZ_mKOZhnKAoeuiWEekt8vpjeoIcB3R394k6-zeC52sk60Q8ErZO5PIokS8ytjD-QK-CIJjIRaGg94KzTMz2d3XXjs/activities' \
--header 'Authorization: Bearer authToken' \
--header 'Content-Type: application/json' \
--data-raw '{
    "type": "message",
    "recipient": {
        "id":"29:1dePA4jI3vyrUt-38_--KJxVeAg_v-XQVCBHcSxp4kgu7ZshPPSbsbSOZ7m-GAKK0IARbqyxg_bit-IAt0J9d_w"
    },
    "from": {
        "id": "28:f4016730-f1ef-4fcd-9169-4159a2295591",
        "name": "Toolkit Bot - vTAPBotdemo"
    },
    "channelData": {
        "notification": {
            "alert": "true"
        }
    },
    "text": "Hello  "
}'

Below is the error message i receive when triggering the above api. enter image description here Please let me know how to resolve this issue


Solution

  • The endpoint i used to postMessage on personal chat is incorrect and it was present in the docs as well.

    Below is the correct endpoint. This endpoint can be seen by logging the http payload request body of /api/messages api

    curl --location --request POST 'https://smba.trafficmanager.net/amer/v3/conversations/{conversationid}/activities/{activityid}' \
    --header 'Authorization: Bearer authToken' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "type": "message",
        "recipient": {
            "id":"29:1dePA4jI3vyrUt-38_--VeAgKJx_v-CBHXQVcSxp4kgu7ZshPPSbsbSOZ7m-GAKK0IARbqyxg_bit-IAt0J9d_w"
        },
        "from": {
            "id": "28:f4016730-f1ef-4fcd-9169-4159a2295591",
            "name": "Toolkit Bot - vTAPBotdemo"
        },
        "channelData": {
            "notification": {
                "alert": "true"
            }
        },
        "text": "Hello  "
    }'