node.js - 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 代码有什么问题?
谢谢,
迭戈
解决方案
您信任该服务吗?根据您的代码,它不这么认为,在您的情况下,这是 401 的典型原因。
在 node.js 中,执行以下操作:
MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
如果您想了解更多详细信息,请查看有关在此处发送主动消息时获取 401 的文档
还有这个关于团队和主动消息传递的 SO 答案,特别是最后一个块。 Teams 中的主动消息传递机器人,无需事先提及该机器人
推荐阅读
- python-3.x - 本机 python3 无法在 ubuntu 焦点上导入 sqlite3
- node.js - 为什么 mongodump 不能在我的 Windows 机器上运行?
- angular - 从 HttpClient Angular 提交文件到 API-Rest CodeIgniter 3
- amazon-web-services - AWS Lambda 为单个 SQS 消息触发了两次
- python - 努力让小部件在 python 中工作
- sql - 按特定顺序对列值求和
- java - 如何从日历中仅获取没有时间的日期?
- sql-server - SQL Server:如果搜索不匹配则返回一个空行
- java - 即使 hashmap get(key) 不遵循定义的泛型,也不会抛出异常
- flutter - 包导入失败“URI 目标不存在:import 'package:supercharged/supercharged.dart';”