从Bot Nodejs的Teams下载附件

时间:2018-11-01 22:10:49

标签: botframework

当我尝试使用Bot Builder v4在Microsoft Teams中获取附件时,遇到以下错误:

{“消息”:“此请求的授权已被拒绝。”}

在版本3中,一切正常,据我在Teams中所知,必须有一个令牌才能获取文件的二进制数组。 在v3中,我可以通过以下方式获取jwt令牌: connector.getAccessToken.bind(connector)

然后在GET request的标头中使用它=>

headers: {
            'Authorization': 'Bearer ' + token,
            'Content-Type': 'application/octet-stream'
         }

在v4中: context.adapter.getUserToken(step.context,CONNECTION_SETTING_NAME);

还有另一种在v4中获取有效令牌的方法吗?

2 个答案:

答案 0 :(得分:0)

要获取令牌,只需再次调用提示。您可以找到auth sample here for节点。该示例恰好使用瀑布对话框,在您的情况下可能不需要

let prompt = await step.prompt(OAUTH_PROMPT);

如果令牌有效且未过期,则可以如下所示获取令牌;如果令牌无效或用户没有令牌,则会提示他们登录。否则,令牌将出现在提示结果中

var tokenResponse = prompt.result;
if (tokenResponse != null) {
    await step.context.sendActivity(`Here is your token: ${ tokenResponse.token }`);
}

样本中的这些评论应有助于解释

// Call the prompt again because we need the token. The reasons for this are:
// 1. If the user is already logged in we do not need to store the token locally in the bot and worry
// about refreshing it. We can always just call the prompt again to get the token.
// 2. We never know how long it will take a user to respond. By the time the
// user responds the token may have expired. The user would then be prompted to login again.
//
// There is no reason to store the token locally in the bot because we can always just call
// the OAuth prompt to get the token or get a new token if needed.

答案 1 :(得分:0)

这些步骤

x_scaled