You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Microsoft Graph API发送Teams私聊消息遇权限错误求助

Teams私聊消息API调用异常:创建聊天成功但发送消息返回Unauthorized

公司计划开发可在Teams私聊中发送消息的API,采用Microsoft Graph API实现。参考StackOverflow方案,创建聊天并发送消息的流程中,创建聊天步骤可成功获取聊天ID,但发送消息步骤报错。

操作步骤

1. 创建Azure应用并配置权限

(权限截图见链接)

2. 创建一对一聊天并获取ID

代码实现如下:

var user_email = IT.DsEmail;

var chat = new Chat
{
    ChatType = ChatType.OneOnOne,
    Members = new ChatMembersCollectionPage()
    {
        new AadUserConversationMember
        {
            Roles = new List<String>()
            {
                "owner"
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"user@odata.bind", "https://graph.microsoft.com/v1.0/users('notificacoes@vaxxinova.com.br')"}
            }
        },
        new AadUserConversationMember
        {
            Roles = new List<String>()
            {
                "owner"
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"user@odata.bind", "https://graph.microsoft.com/v1.0/users('" + user_email + "')"}
            }
        }
    }
};
var user = await graphServiceClient.Chats.Request().AddAsync(chat);

此步骤执行成功,可正常获取聊天ID。

3. 发送私聊消息

代码实现如下:

var chatMessage = new ChatMessage
{
    Body = new ItemBody
    {
        Content = "Hello world"
    }
};

await graphServiceClient.Chats["19:35fa8078-de1a-4ec1-bb8f-85f1d4e14fa9_8848a24c-c679-415c-ac14-7f9dde86d6ec@unq.gbl.spaces"].Messages
    .Request()
    .AddAsync(chatMessage);

执行时收到错误:

Unauthorized
Message: Message POST is allowed in application-only context only for import purposes. Refer to相关文档了解详情。

问题

为何创建聊天成功但发送消息失败?请求协助解决。

内容的提问来源于stack exchange,提问作者Julia Cassemiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 14:15:26