如何在 Microsoft Teams Bot 中进行身份验证后获取用户访问令牌?
How to fetch user access token after authentication in Microsoft Teams Bot?
我正在开发我的第一个 bot from Microsoft Teams。
我希望用户在机器人中输入命令,机器人应该向我的外部网络服务器发送请求并将结果显示为自适应卡片。我能够使用我的外部服务器对机器人进行身份验证。机器人在身份验证后显示用户访问令牌。完美!
如何在我的机器人代码或 Web 服务器中获取用户的访问令牌以处理来自机器人的传入请求。这是我的机器人代码的样子。
this.onMessage(async (context, next) => {
//I need a way to get the user's access token here
//or a way to fetch the access token from my web server
//based on some id in the context.
const response = await myWebService.getData(context);
// Run the Dialog with the new message Activity.
await this.dialog.run(context, this.dialogState);
await next();
});
我在这里错过了什么?
您可以在登录过程中捕获令牌。假设您的登录过程结构如下所示,用户登录的结果从 promptStep()
传递到 loginStep()
。它在 stepContext.result
中可用,我已将其作为 activity.
中的文本分配给 tokenResponse
和 return 给用户
在这里您可以执行您需要的额外逻辑。
async promptStep(stepContext) {
return await stepContext.beginDialog(OAUTH_AAD_PROMPT);
}
async loginStep(stepContext) {
// Get the token from the previous step. Note that we could also have gotten the
// token directly from the prompt itself. There is an example of this in the next method.
const tokenResponse = stepContext.result;
if (tokenResponse) {
return await stepContext.context.sendActivity(`Your token is: ${ tokenResponse.token }`);
}
await stepContext.context.sendActivity('Login was not successful, please try again.');
return await stepContext.next();
}
希望得到帮助!
BotFrameworkAdapter 的 GetUserTokenAsync 可用于在任何瀑布步骤中获取令牌,如下所示
var botAdapter = (BotFrameworkAdapter)stepContext.Context.Adapter;
TokenResponse result = await botAdapter.GetUserTokenAsync(stepContext.Context, connectionName, null, cancellationToken);
我正在开发我的第一个 bot from Microsoft Teams。
我希望用户在机器人中输入命令,机器人应该向我的外部网络服务器发送请求并将结果显示为自适应卡片。我能够使用我的外部服务器对机器人进行身份验证。机器人在身份验证后显示用户访问令牌。完美!
如何在我的机器人代码或 Web 服务器中获取用户的访问令牌以处理来自机器人的传入请求。这是我的机器人代码的样子。
this.onMessage(async (context, next) => {
//I need a way to get the user's access token here
//or a way to fetch the access token from my web server
//based on some id in the context.
const response = await myWebService.getData(context);
// Run the Dialog with the new message Activity.
await this.dialog.run(context, this.dialogState);
await next();
});
我在这里错过了什么?
您可以在登录过程中捕获令牌。假设您的登录过程结构如下所示,用户登录的结果从 promptStep()
传递到 loginStep()
。它在 stepContext.result
中可用,我已将其作为 activity.
tokenResponse
和 return 给用户
在这里您可以执行您需要的额外逻辑。
async promptStep(stepContext) {
return await stepContext.beginDialog(OAUTH_AAD_PROMPT);
}
async loginStep(stepContext) {
// Get the token from the previous step. Note that we could also have gotten the
// token directly from the prompt itself. There is an example of this in the next method.
const tokenResponse = stepContext.result;
if (tokenResponse) {
return await stepContext.context.sendActivity(`Your token is: ${ tokenResponse.token }`);
}
await stepContext.context.sendActivity('Login was not successful, please try again.');
return await stepContext.next();
}
希望得到帮助!
BotFrameworkAdapter 的 GetUserTokenAsync 可用于在任何瀑布步骤中获取令牌,如下所示
var botAdapter = (BotFrameworkAdapter)stepContext.Context.Adapter;
TokenResponse result = await botAdapter.GetUserTokenAsync(stepContext.Context, connectionName, null, cancellationToken);