首页 > 解决方案 > node.js 中的 TEAMS 机器人:CreateConversation 方法中的“此请求的授权已被拒绝”

问题描述

我有一个在本地运行的 TEAMS node.js 机器人(使用 ngrok)。我收到来自 TEAMS 客户端的消息并回显工作

context.sendActivity(`You said '${context.activity.text}'`);

现在我想向该用户发送 1to1 消息,但我收到

Error: Authorization has been denied for this request

创建对话时。

我的代码:

   var sUserId  = "29:1shb_5I6CkkerBVq4qPqcv5dGwDfkXx11Jbjc1UnGCIv"
   var sServiceUrl = "https://smba.trafficmanager.net/emea/";
   var sTenantId = "942369d2-208e-438b-894c-0d0e1510cf61";
   var credentials = new BotConnector.MicrosoftAppCredentials({
        appId: "xxxxxxx",
        appPassword: "yyyyyyyy"
    });
    var connectorClient = new BotConnector.ConnectorClient(credentials, { baseUri: sServiceUrl });

    const parameters = {
        members: [ { id: sUserId } ],
        isGroup: false,
        channelData:
        {
            tenant: {
                id: sTenantId
            }
        }
    };
    var conversationResource = await connectorClient.conversations.createConversation(parameters);

// I get the error here, next is not executed
    await connectorClient.conversations.sendToConversation(conversationResource.id, {
        type: "message",
        from: { id: "xxxxxxx" },
        recipient: { id: sUserId },
        text: 'This a message from Bot Connector Client (NodeJS)'
    });

appId 和 appPassword 有效(来自 .env 文件),如果它们错误,我将无法接收来自 TEAMS 客户端的消息

我有相同的代码在 .NET 机器人中创建对话,它适用于我:

  var parameters = new ConversationParameters
   {
       Members = new[] { new ChannelAccount(sUserId) },
       ChannelData = new TeamsChannelData
       {
            Tenant = new TenantInfo(sTenantId),
       },
   };

   retValue = await connectorClient.Conversations.CreateConversationAsync(parameters);

我的 node.js 代码有什么问题?

谢谢,

迭戈

标签: node.jsauthorizationbotframeworkmicrosoft-teams

解决方案


您信任该服务吗?根据您的代码,它不这么认为,在您的情况下,这是 401 的典型原因。

在 node.js 中,执行以下操作:

MicrosoftAppCredentials.trustServiceUrl(serviceUrl);

如果您想了解更多详细信息,请查看有关在此处发送主动消息时获取 401 的文档

还有这个关于团队和主动消息传递的 SO 答案,特别是最后一个块。 Teams 中的主动消息传递机器人,无需事先提及该机器人


推荐阅读