如何使用 Azure ClientApplication Token 向 Microsoft Teams 发送消息

How to write a message to Microsoft Teams with Azure ClientApplication Token

我尝试使用图形 api 和非个人 ClientApplication 令牌向 Microsoft Teams 发送消息。

目前通过clientId/secret/tenantId从Azure获取token是没有问题的。该令牌似乎也能正常工作,所以我能够得到例如rest api 所有 azur 用户的列表。但是当我尝试使用此令牌 (POST https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/messages) 向 Teams 发送消息时,结果是 { “错误”: { "code": "未经授权", "message": "未经授权", “内部错误”:{ “日期”: ”...”, “请求 ID”:“……” } } }

我的个人不记名令牌可以正常调用相同的呼叫。

知道如何让客户端应用程序 post 加入团队吗?

根据您的描述,您正在使用 client credential flow 获取令牌,然后使用令牌调用 Microsoft Graph,如果是这样,则需要 Application permission

您遇到错误是因为 Microsoft Graph - Create chatMessage in a channel 不支持 Application permission,这意味着在这种情况下您无法使用客户端凭据流。


常见问题解答:

The token seems to work as well, so I'm able to get e.g. the list of all azure users by rest api.

要通过此流程调用不同的 API,您需要不同的 Application permission,这是可行的,因为您的应用具有 application permission to call the api - List users.

The same call works fine with my personal bearer token.

我想您正在使用另一个流程来获取令牌,例如auth code flow, ropc flow. These flows need the Delegated permission, which is supported to create chatMessage in a channel.