How to send teams message 1:1 using graph API to new user

63083710 26 Reputation points
2022-02-09T13:30:37.213+00:00

i want to send teams message from my MS access VBA:

-i have created an application on Azure portal (SendMessageFromDMH)

  • i have logged in to office using my Microsoft user : "Mohamed.s-yassin@qnbalahli.com"
    -i have followed the steps to get the authorization code : "https://login.microsoftonline.com/{Application Directory (tenant) ID}/oauth2/v2.0/authorize?"
    -i have followed the steps to get the access_token : "https://login.microsoftonline.com/{Application Directory (tenant) ID}/oauth2/v2.0/token?"
    -i have followed the steps to send chat message to existing chat : "https://graph.microsoft.com/v1.0/chats/{chat id}/messages" --- "hello world"

-the problem that i want to send message to new user (not existing chat ) by following :
-create chat as : https://learn.microsoft.com/en-us/graph/api/chat-post?view=graph-rest-1.0&tabs=http

"https://graph.microsoft.com/v1.0/chats"

-
{ "chatType": "oneOnOne",
"members": [
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"user@odata.bind": "https://graph.microsoft.com/v1.0/users('Mohamed.s-yassin@qnbalahli.onmicrosoft.com')" ' this for Mohamed.s-yassin@qnbalahli.com
},
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"user@odata.bind": "https://graph.microsoft.com/v1.0/users('moyassinomar@onmicrosoft.com')" ' this for moyassinomar@Stuff .com
}
]
}
but it gives me below error :
{"error":{"code":"Unauthorized","message":"Failed to make request to Azure Active directory","innerError":{"date":"2022-02-09T13:25:45","request-id":"b64280bc-1e2e-4989-9cf1-c485bf2088e8","client-request-id":"b64280bc-1e2e-4989-9cf1-c485bf2088e8"}}}

could you support how to solve this error and guide me hot send the message to non exisiting chat?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,646 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,868 questions
{count} votes

2 answers

Sort by: Newest
  1. Denis Iongbloed 0 Reputation points
    2023-06-14T10:17:10.15+00:00

    Hi guys....

    Just for add the MS link about it here for others:

    https://learn.microsoft.com/en-us/graph/api/chat-post?view=graph-rest-1.0&tabs=http

    Here you can see all about the permissions, create, update, send chat messages to owners and guest persons.

    Seeya,

    Denis

    0 comments No comments

  2. HarmeetSingh7172 4,811 Reputation points
    2022-02-09T15:35:19.137+00:00

    Hi MohamedSYASSIN-5189,

    I am able to replicate your issue at my end.

    Please find the below screenshots where I tried to replicate your issue.

    Step 1- I have invited guest user to the chat by passing the user’s email address. After executing the send chat message endpoint I faced the same issue as similar as your case.

    172558-ss2.png

    Step 2- To resolve this issue, I got the guest users’ user-id and I passed that in request body for one-to-one chat. Then the request is successful.
    172653-ss3.png

    As per MS documentation, the API can not create a new chat message.
    You must use the list chats method to retrieve the ID of an existing chat before you can create a chat message.

    Hope this helps.

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