How to send a message to MS teams from standalone application
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:
- 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);
- Created graph client object
final GraphServiceClient<Request> graphClient = GraphServiceClient .builder() .authenticationProvider(tokenCredentialAuthProvider) .buildClient();
- 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