0
votes

I have a scenario for sending notification into a chat window from my microsoft teams custom tab. So, I am trying to call Microsoft Graph API for sending message into a specific chatId: Post https://graph.microsoft.com/beta/chats/${chatId}/messages I can get access_token successfully , but while calling post request for sending notification into a chatId , I get "401 UnAuthorized" error . what is wrong with my code?

var message = {
            "body": {
                   "content": "Hello World"
                }
               };
                    fetch(`https://graph.microsoft.com/beta/${context.userObjectId}/chats/${chatId}/messages`, {
                        method: "POST",
                        mode: "cors",
                        cache: "no-cache",
                        headers: {
                           
                            'Content-Type': "application/json;charset=utf-8",
                            'Authorization': `Bearer ${fetched_accessToken}`
                        },
                        body: JSON.stringify(message)
                    }).then((response) => {
                         alert(response.statusText);
                    }).catch((err) => {
                       alert(err);
                    });    
                } 

what is wrong with this code?

2
I tried to use FormData as the body, but I got "Network error" let data = new FormData(); data.append('client_id', 'xxxxx'); data.append('scope', 'graph.microsoft.com/.default'); data.append('client_secret', 'xxxx'); data.append('grant_type', 'client_credentials');maryam gharibi
well, I could fetch the access_token , by calling the body parameters in deferent way: const data = "client_id=xxxxxx&scope=graph.microsoft.com/.default&client_secret =xxxxxxx&grant_type=client_credentials"; but still I get 404 (Unauthorized) error while posting the message into chat windowmaryam gharibi
I see that you're trying to use Client Credential flow(application permissions), but the above API call will only work the delegated permissions. It's "by design" and designed to work in that way only. Hence you're seeing the error. You can try the above API call with delegated permissions and you will see it will work. Here's the documentation.Dev
Let me know if it helps.Dev

2 Answers

1
votes

If you have a Teams app already, for your tab, and you want to ALSO communicate into the chat, you should look into creating a bot, and using Proactive messaging. See more at https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/send-proactive-messages?tabs=dotnet and https://docs.microsoft.com/en-us/graph/teams-proactive-messaging

0
votes
  • I see that you're trying to use Client Credential flow(application permissions), but the above API call will only work the delegated permissions. Here's the documentation. So it's "by design" and designed to work in that way only. Hence you're seeing the error.
  • You can try the above API call with delegated permissions and you will see it will work.