How to send a message to MS teams from standalone application

Supriya Saraogi 1 Reputation point
2022-01-28T19:50:07.58+00:00

I'm trying to send message to MS teams channel from my application. The documentation sending chat message to msteams channel is all over the place. It's not helpful for developers. I understand that to send message to MS teams first I'll have to configure an auth provider.

Things tried so far:

  1. created an auth provider. In my case I used client auth provider
 // Create an auth provider
      final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
          .clientId(vaultSecretProvider.getSecret(MSTEAMS_CLIENTID))
          .clientSecret(vaultSecretProvider.getSecret(MSTEAMS_CLIENT_SECRET))
          .tenantId(vaultSecretProvider.getSecret(MSTEAMS_TENANTID))
          .build();

      List<String> scopes = Arrays.asList(MSTEAMS_SCOPE);

      final TokenCredentialAuthProvider tokenCredentialAuthProvider =
          new TokenCredentialAuthProvider(scopes, clientSecretCredential);
  1. Created graph client object
           final GraphServiceClient<Request> graphClient =
              GraphServiceClient
                  .builder()
                  .authenticationProvider(tokenCredentialAuthProvider)
                  .buildClient();
    
  2. Trying to send chat message to Ms Teams
            graphClient.teams("groupId")
              .channels("ChannelId").messages()
              .buildRequest()
              .post(chatMessage);
    

When I'm trying to send the message the code gets stuck at AuthenticationHandler.java this is part of - package com.microsoft.graph.httpcore; in line

            final CompletableFuture<String> future = authProvider.getAuthorizationTokenAsync(originalRequest.url().url());

there is no error thrown. Can someone help me understand what am I missing.

PS: I have added the delegated permissions and I'm using my work account to send the request. Also already referred to following documentation https://learn.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-1.0&tabs=java https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=Java

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