【问题标题】:OAuth authentication with Azure Active Directory app使用 Azure Active Directory 应用程序进行 OAuth 身份验证
【发布时间】:2018-07-13 06:32:53
【问题描述】:

我正在尝试在https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-oauth/app.js 的 botbuilder nodejs 文档中运行 OAuth 示例

我已经在 Azure 上设置了 Azure Active Directory v1 应用程序,并使用图形 api 访问,并将 OAuth 连接添加到我的机器人。下面代码中使用的connectionName 已经准备好了。

但是当我运行从文档(上面提供的 github 链接)中获取的代码时,我得到:

TypeError: connector.getUserToken 不是函数

我在模拟器和网络聊天频道上都运行过这个,得到了同样的错误。

// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function (session) {
    if (session.message.text == 'signout') {
        // It is important to have a SignOut intent
        connector.signOutUser(session.message.address, connectionName,  (err, result) => {
            if (!err) {
                session.send('You are signed out.');
            } else {
                session.send('There was a problem signing you out.');                
            }
        });
    } else {
        // First check whether the Azure Bot Service already has a token for this user
        connector.getUserToken(session.message.address, connectionName, undefined, (err, result) => {
            if (result) {
                // If there is already a token, the bot can use it directly
                session.send('You are already signed in with token: ' + result.token);
            } else {
                // If there not is already a token, the bot can send an OAuthCard to have the user log in
                if (!session.userData.activeSignIn) {
                    session.send("Hello! Let's get you signed in!");
                    builder.OAuthCard.create(connector, session, connectionName, "Please sign in", "Sign in", (createSignInErr, signInMessage) =>
                    {
                        if (signInMessage) {
                            session.send(signInMessage);
                            session.userData.activeSignIn = true;
                        } else {
                            session.send("Something went wrong trying to sign you in.");
                        }     
                    });
                } else {
                    // Some clients require a 6 digit code validation so we can check that here
                    session.send("Let's see if that code works...");
                    connector.getUserToken(session.message.address, connectionName, session.message.text, (err2, tokenResponse) => {
                        if (tokenResponse) {
                            session.send('It worked! You are now signed in with token: ' + tokenResponse.token);
                            session.userData.activeSignIn = false;
                        } else {
                            session.send("Hmm, that code wasn't right");
                        }
                    });
                }
            }
        });
    }
})

【问题讨论】:

    标签: node.js botframework


    【解决方案1】:

    根据您的错误信息,能否请您仔细检查一下您本地的botbuiler 版本,因为3.15.0 中添加了getUserToken 功能,您可以在https://github.com/Microsoft/BotBuilder/blob/botbuilder%403.15.0/Node/core/src/bots/ChatConnector.ts 找到定义,它没有显示在此版本之前。

    【讨论】:

    • 谢谢。我现在正在拿到签到卡。当我点击它时,在模拟器日志中,它说“参数“url”必须是一个字符串,而不是未定义的”。不知道该怎么办。
    • 嗨@rajesh,这似乎是模拟器中的一个错误,在Botbuilder的存储库中有一个讨论,请关注问题github.com/Microsoft/BotBuilder/issues/4642以了解进度。
    最近更新 更多