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.
2
Answers
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.
I used this approach and it worked for me.. Thank you Sreedevi for posting this in the forum.
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:
Response:
In my case, I added
ChatMessage.Send
permission of Delegated type in my application like this:Now, I ran below authorization request in browser that gives
code
value in address bar after successful authentication:You can use this
code
value to get access token using authorization code flow via Postman with below parameters:Response:
When I used this token to send message in chat, I got the response successfully like this:
Response: