使用Azure Active Directory应用程序进行OAuth身份验证

时间:2018-07-13 06:32:54

标签: node.js botframework

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

我已经在具有图api访问权限的Azure上设置了Azure Active Directory v1应用程序,并将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");
                        }
                    });
                }
            }
        });
    }
})

1 个答案:

答案 0 :(得分:1)

根据您的错误消息,能否请您仔细检查您的本地botbuiler版本,因为3.15.0中添加了getUserToken函数,您可以在{{3 }},此版本之前不会显示。