在Microsoft Teams Bot中进行身份验证后如何获取用户访问令牌? [英] How to fetch user access token after authentication in Microsoft Teams Bot?

查看:344
本文介绍了在Microsoft Teams Bot中进行身份验证后如何获取用户访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 bot . com/"rel =" nofollow noreferrer> Microsoft团队.

I am developing my first bot from Microsoft Teams.

我希望用户在机器人中输入命令,机器人应将请求发送到我的外部Web服务器,并将结果显示为自适应卡.我可以使用外部服务器对机器人进行身份验证.机器人在身份验证后显示用户访问令牌.完美!

I want the user to input commands in the bot, the bot should send requests to my external web sever and display the results as adaptive cards. I was able to authenticate the bot with my external server. The bot shows the user access token after authentication. Perfect!

如何从我的机器人代码或网络服务器中获取用户的访问令牌,以处理来自机器人的传入请求.这是我的机器人代码的样子.

How can I get the user's access token in my bot code or web server to process the incoming request from the bot. Here's what my bot code looks like.

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().在我已分配给tokenResponse并在活动中以文本形式返回给用户的stepContext.result中可用.

You can capture the token during the login process. Assuming you have structured your login process something like the below, the result of the user logging in is passed from the promptStep() to the loginStep(). It is available in stepContext.result which I have assigned to tokenResponse and return to the user as text in an activity.

您可以在这里执行所需的其他逻辑.

It is here that you can perform the additional logic you need to.

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();
}

希望有帮助!

这篇关于在Microsoft Teams Bot中进行身份验证后如何获取用户访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆