gpt4 book ai didi

botframework - 团队机器人授权 : oAuth card prompts for login every hour with AADV2

转载 作者:行者123 更新时间:2023-12-04 15:24:38 28 4
gpt4 key购买 nike

我正在关注 teams auth sample在这里可用。要使用带有 AADV2 连接的 oAuth Prompt 登录机器人,会收到 token ,但大约一小时后,机器人会再次提示“登录”,根据我的理解,机器人应该静默获取新 token ,而不是提示用户登录。

在示例中,它提到了 this但不确定这到底是什么意思。

由于用户已经登录,我们如何在不再次登录提示的情况下获取新 token ?

最佳答案

该机器人并非设计用于自动刷新 token 。这是开发人员检查 token 是否有效/未过期,如果有效,请请求一个新 token (重新提示登录)。您在第二个链接中引用的代码行展示了如何不在每一轮都将提示发送给用户(这对用户来说会变得非常乏味),而是在每一轮检查 token 是否有效。在 token 有效的那一小时,它返回有效 token 并且用户无论如何都不受影响。 token 过期后,因为这个检查,oauthprompt 卡被重新发送给用户重新登录。

如果用户在上一步的提示中选择"is",则以下方法将运行。然后获取该事件 (const result = stepContext.result),并将其发送回 OAuthPrompt 以查看其上是否有有效 token 。 :

async displayTokenPhase1(stepContext) {
await stepContext.context.sendActivity('Thank you.');

const result = stepContext.result;
if (result) {
// 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.
return await stepContext.beginDialog(OAUTH_PROMPT);
}
return await stepContext.endDialog();
}

OauthPrompt 的构造函数的一部分正在检查是否已经存在有效 token :

/**
* Creates a new OAuthPrompt instance.
* @param dialogId Unique ID of the dialog within its parent `DialogSet` or `ComponentDialog`.
* @param settings Settings used to configure the prompt.
* @param validator (Optional) validator that will be called each time the user responds to the prompt.
*/
constructor(dialogId: string, private settings: OAuthPromptSettings, private validator?: PromptValidator<TokenResponse>) {
super(dialogId);
}

public async beginDialog(dc: DialogContext, options?: PromptOptions): Promise<DialogTurnResult> {
// Ensure prompts have input hint set
const o: Partial<PromptOptions> = {...options};
if (o.prompt && typeof o.prompt === 'object' && typeof o.prompt.inputHint !== 'string') {
o.prompt.inputHint = InputHints.ExpectingInput;
}
if (o.retryPrompt && typeof o.retryPrompt === 'object' && typeof o.retryPrompt.inputHint !== 'string') {
o.retryPrompt.inputHint = InputHints.ExpectingInput;
}

// Initialize prompt state
const timeout: number = typeof this.settings.timeout === 'number' ? this.settings.timeout : 54000000;
const state: OAuthPromptState = dc.activeDialog.state as OAuthPromptState;
state.state = {};
state.options = o;
state.expires = new Date().getTime() + timeout;

// Attempt to get the users token
const output: TokenResponse = await this.getUserToken(dc.context);
if (output !== undefined) {
// Return token
return await dc.endDialog(output);
} else {
// Prompt user to login
await this.sendOAuthCardAsync(dc.context, state.options.prompt);

return Dialog.EndOfTurn;
}
}

评论对此不是很清楚。为了清楚起见,我重写了其中的一些内容,并将与同事联系以解决此问题。

// 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. 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.
// 3. If the token is expired, OAuthPrompt will send a new login card to the user to login, and get a new token

关于botframework - 团队机器人授权 : oAuth card prompts for login every hour with AADV2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62527519/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com