Unable to send chatMessage to ms teams channel using microsoft graph APIs

Supriya Saraogi 1 Reputation point
2021-12-01T01:15:48.49+00:00

I have been trying to send message to teams channel from my web service.
Background. - I have created an outgoing webhook in MS teams sandbox. I am able to receive the request from webhook to my service, but when I'm trying to reply back with chatMessage the post Messsage doesn't seem to work.

I'm trying to send the message using Microsoft graph API. To access MS graph API. I have created an app in Azure AD account. Added following permissions to the app -

  • ChannelMessage.Read.All, ChannelMessage.Send, Chat.ReadWrite, ChatMessage.Send, Teamwork.Migrate.All, Files.ReadAll, Group.ReadWrite.All, User.Read, User.Read.All, Application.ReadWrite.OwnedBy

I'm using client_credentials Auth provider. When I try to get the token via postman using clientID, client_secret, grant_type and scope. I get the bearer token. But When I'm trying to get the same token and send request to teams using SDK it doesn't seem to work.

Here's the code snippet

public boolean postMessage() {
try {
// Create an auth provider
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(CLIENTID)
.clientSecret(vaultSecretProvider.getSecret(CLIENT_SECRET)
.tenantId(TENANTID)
.build();

  List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default");

  final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, clientSecretCredential);

  //Create request body
  ChatMessage chatMessage = new ChatMessage();
  ItemBody body = new ItemBody();
  body.content = "Hello World";
  chatMessage.body = body;

  //Create graph client object to send request to teams channel
  final GraphServiceClient<Request> graphClient =
      GraphServiceClient
          .builder()
          .authenticationProvider(tokenCredentialAuthProvider)
          .buildClient();

  //post message to teams channel
  graphClient.teams(teamsId) // got from get link to teams- groupId
      .channels(channelId).messages() // received in request
      .buildRequest()
      .post(chatMessage);
} catch (Exception e) {
  log.error("Error: " + e);
}
return false;

}

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,063 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,581 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,842 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ramjot Singh 861 Reputation points Microsoft Employee
    2021-12-03T09:52:40.257+00:00

    Post message supports Application-only permissions only for migration scenarios (which requires team and channels to be in a locked state). Otherwise the API does not support application permissions. Refer to documentation for more details https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http


  2. Claudia Robles 1 Reputation point
    2021-12-07T00:31:41.173+00:00

    Thank you for the feedback. Supriya will take a look at the info. once she is back next week and let you know if this solves the issue.

    0 comments No comments