I want to send a proactive message with a team bot. For that I:
- created a bot in the bot framework
- created an app with AppStudio in teams
- assigned the bot to my app
- assigned the app to my team
Now I would like to send a request to this Url:
https://smba.trafficmanager.net/de/v3/conversations/{teamId}/activities/
to send a message to the channel. For this I have to get a Bearer Token with this Token Url:
https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token
I get this back without any problems.
Now I want to use the GraphAPI to execute a custom request
First of all I have to create a GraphServiceClient like this:
OAuth2AuthenticationProvider authProvider = new OAuth2AuthenticationProvider(_getBotToken(botClientId, botClientSecret, BOT_SCOPE));
graphBotClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
After I created the client I do the request.
graphBotClient.customRequest("https://smba.trafficmanager.net/de/v3/conversations/" + pTeamId + "/activities").buildRequest().post(messageAsJson);
I need to use a custom query because there are no methods for it in Java. messageAsJson is a Json object that looks like an activity object, which is needed to write messages.
{
"type": "message",
"from": {
"id": "{clientBotIdFromAzure}",
"name": "Botname"
},
"conversation": {
"id": "{teamId}",
"name": "ChannelName"
},
"text": "My bot's reply"
}
When I execute this command I get the following exception:
401 : Unauthorized
Strict transport security : max-age=31536000
Cache control : private
x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter": "West Europe", "Slice": "SliceC", "Ring": "5", "ScaleUnit": "002", "RoleInstance": "AGSFE_IN_102"}}
client-request-id : 9a7197en-dd0f-4c90-8b2b-8dc5bb1200ee
WWW-Authenticate : Bearer realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize", client_id="00000003-0000-0000-c000-000000000000"
request-id : b07b1399-7175-40d3-9891-266abe1144b9
Content-Length : 262
Date : Wed, 22 Apr 2020 08:24:28 GMT
Content-Type : application/json
{
"error." {
"code." "InvalidAuthenticationToken"
"message": "Access token validation failure. Invalid audience."
"innerError": {
"request-id": "b07b1399-7175-40d3-9891-266abe1144b9"
"date": "2020-04-22T08:24:29"
}
}
}
Exception in thread "main" com.microsoft.graph.http.GraphServiceException: Error code: InvalidAuthenticationToken
Error message: Access token validation failure. Invalid audience.
If I execute the same request in Postman, with the same token, url and body, the request works fine and my message appears in MSTeams Message sent out of Postman appears in MSTeams
Why does it not work with the GraphAPI? Where is my mistake? What am I missing?
Many greetings