How to send teams message 1:1 using graph API by MS access vba

63083710 26 Reputation points
2022-02-13T07:36:54.707+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 (existing chat or non 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('amr.AMIEN@qnbalahli.com')" ' this for amr.AMIEN@qnbalahli.com
}
]
}
-i could successfully send messages to user "amr.AMIEN@qnbalahli.com" or "shehab.hagag@qnbalahli.com".

-but it gives me below error when i try to send message to "moyasssinomar@Stuff .com" or "dmh.robot@Stuff .com":
{"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 ?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,521 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,826 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Ramjot Singh 861 Reputation points Microsoft Employee
    2022-02-14T06:59:01.847+00:00

    There are 2 aspects here. Let me try to break both of them down and suggest for you a path forward.

    Considering you have a chat with them already, I am going to assume they have been added to the tenant already as guest.

    The API you are using /users('') does not take email, it instead takes userPrincipalName(upn). When guests are added to the tenant, they get assigned a upn. Additionally, even for in tenant users, email and upn can differ (although upn will still work as email and email and upn are same if separate email has not been created).

    So you have 2 options here.

    1. Either get the upn of the said users
    2. Or get the userId for them

    Both of those you can get from /users call (you can use filters). Refer to https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http

    Once you have the right details, you should be able to create the chat.


  2. 63083710 26 Reputation points
    2022-02-22T15:14:35.513+00:00

    i have successfully invited the user "dmh.robot@Stuff .com" and get user id "fc6314a3-bee0-41ab-8ca2-ef6b2ba213a5" and then created chat id "19:ea331eb9-a2ee-43c8-9425-4fcfb7ac20c8_fc6314a3-bee0-41ab-8ca2-ef6b2ba213a5@unq.gbl.spaces" and successfully received response 201 (message sent).
    But unfortunately it looks strange in the web portal as attached and not received on teams account "dmh.robot@Stuff .com"

    noting that the original chat id with dmh.robot is "19:uni01_e3qvikkx2cq6goxbotb5uuvsmo37kspj4awt3vlqlomodm7f5jha@thread.v2?ctx=chat"
    176912-image.png