How to send chat message using graph api using access token generated using application credentials

pega notifications 0 Reputation points
2024-02-06T11:04:31.8666667+00:00

I am generating access token using application credentials. And I am using same token to send message to chat. I am getting "Unauthorized" error. But same token is working to create chat, schedule meeting,. etc. I have checked permissions. I have given all relevant permissions("Teamwork.Migrate.All" to send chat message) to my application

Microsoft Teams Development
Microsoft Security Microsoft Graph
Microsoft Teams Microsoft Teams for business Other
{count} votes

2 answers

Sort by: Most helpful
  1. Vikram Boorla (Tata Consultancy Services Limi) 250 Reputation points Microsoft External Staff
    2024-02-07T06:27:07.0666667+00:00

    Hello @pega notifications
    1.Check the required permissions: Ensure that you have granted the necessary permissions to your application. In your case, you mentioned that you have given the "Teamwork.Migrate.All" permission to your application. However, it's important to note that this permission is not specifically for sending chat messages. Make sure you have the appropriate permission for sending chat messages, such as "ChatMessage.Send" or "Chat.ReadWrite". You can refer to the Microsoft Graph permissions reference documentation for more details on choosing the correct permissions.

    2.Verify the token scope: When generating the access token, make sure that you are requesting the correct scope that includes the necessary permissions for sending chat messages. Double-check the scope parameter in your token request to ensure it includes the required permissions.

    3.Check the API endpoint: Ensure that you are using the correct API endpoint to send the chat message. The endpoint should be https://graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages where {teamId} is the ID of the team and {channelId} is the ID of the channel where you want to send the message.

    4.Verify the token authentication: Ensure that you are correctly including the access token in the authorization header of your API request. The header should be in the format Authorization: Bearer {access_token} where {access_token} is the token you generated.

    0 comments No comments

  2. CarlZhao-MSFT 46,371 Reputation points
    2024-02-07T09:25:38.3033333+00:00

    Hi @pega notifications

    According to the documentation: Application permissions are only supported for migration. This means you must create teams and channels in a migrated state and then import third-party messages.

    User's image

    If you just want to send messages in a channel or chat without migrating data, use delegated permissions instead.

    Hope this helps.

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.