Microsoft Teams 机器人 - link 展开身份验证流程
Microsoft Teams bot - link unfurling auth flow
找不到 link 展开的身份验证流程的良好示例。我设法使用 this example 运行 oauth 流程。但是在用户提供登录名和密码并且机器人第二次点击 OnTeamsAppBasedLinkQueryAsync
之后 GetUserTokenAsync
仍然 returns 为空。因此,我不遵循身份验证流程完成后应该从哪里获取令牌。我应该以某种方式坚持下去吗? Teams 会根据每个请求向我发送令牌吗?它应该如何工作?
所以在我的例子中,下面的代码总是 returns null:
var tokenResponse = await (turnContext.Adapter as IUserTokenProvider)
.GetUserTokenAsync(turnContext, _connectionName, default(string),
cancellationToken: cancellationToken);
AppBasedLinkQuery
上似乎没有 'state' 字段。身份验证流程完成后,将再次调用 OnTeamsAppBasedLinkQueryAsync
,并且 turnContext.Activity.Value
将包含 url 和 'state'(或魔术代码)。我们会将此字段添加到 AppBasedLinkQuery
(在此处创建问题:microsoft/botbuilder-dotnet#3429)。
解决方法是直接从 Activity.Value
中检索 state/magiccode 类似于:
protected async override Task<MessagingExtensionResponse> OnTeamsAppBasedLinkQueryAsync(ITurnContext<IInvokeActivity> turnContext, AppBasedLinkQuery query, CancellationToken cancellationToken)
{
var magicCode = string.Empty;
var state = (turnContext.Activity.Value as Newtonsoft.Json.Linq.JObject).Value<string>("state");
if (!string.IsNullOrEmpty(state))
{
int parsed = 0;
if (int.TryParse(state, out parsed))
{
magicCode = parsed.ToString();
}
}
var tokenResponse = await(turnContext.Adapter as IUserTokenProvider).GetUserTokenAsync(turnContext, _connectionName, magicCode, cancellationToken: cancellationToken);
if (tokenResponse == null || string.IsNullOrEmpty(tokenResponse.Token))
{
// There is no token, so the user has not signed in yet.
// Retrieve the OAuth Sign in Link to use in the MessagingExtensionResult Suggested Actions
var signInLink = await(turnContext.Adapter as IUserTokenProvider).GetOauthSignInLinkAsync(turnContext, _connectionName, cancellationToken);
return new MessagingExtensionResponse
{
ComposeExtension = new MessagingExtensionResult
{
Type = "auth",
SuggestedActions = new MessagingExtensionSuggestedAction
{
Actions = new List<CardAction>
{
new CardAction
{
Type = ActionTypes.OpenUrl,
Value = signInLink,
Title = "Bot Service OAuth",
},
},
},
},
};
}
var heroCard = new ThumbnailCard
{
Title = "Thumbnail Card",
Text = query.Url,
Images = new List<CardImage> { new CardImage("https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png") },
};
var attachments = new MessagingExtensionAttachment(HeroCard.ContentType, null, heroCard);
var result = new MessagingExtensionResult("list", "result", new[] { attachments });
return new MessagingExtensionResponse(result);
}
找不到 link 展开的身份验证流程的良好示例。我设法使用 this example 运行 oauth 流程。但是在用户提供登录名和密码并且机器人第二次点击 OnTeamsAppBasedLinkQueryAsync
之后 GetUserTokenAsync
仍然 returns 为空。因此,我不遵循身份验证流程完成后应该从哪里获取令牌。我应该以某种方式坚持下去吗? Teams 会根据每个请求向我发送令牌吗?它应该如何工作?
所以在我的例子中,下面的代码总是 returns null:
var tokenResponse = await (turnContext.Adapter as IUserTokenProvider)
.GetUserTokenAsync(turnContext, _connectionName, default(string),
cancellationToken: cancellationToken);
AppBasedLinkQuery
上似乎没有 'state' 字段。身份验证流程完成后,将再次调用 OnTeamsAppBasedLinkQueryAsync
,并且 turnContext.Activity.Value
将包含 url 和 'state'(或魔术代码)。我们会将此字段添加到 AppBasedLinkQuery
(在此处创建问题:microsoft/botbuilder-dotnet#3429)。
解决方法是直接从 Activity.Value
中检索 state/magiccode 类似于:
protected async override Task<MessagingExtensionResponse> OnTeamsAppBasedLinkQueryAsync(ITurnContext<IInvokeActivity> turnContext, AppBasedLinkQuery query, CancellationToken cancellationToken)
{
var magicCode = string.Empty;
var state = (turnContext.Activity.Value as Newtonsoft.Json.Linq.JObject).Value<string>("state");
if (!string.IsNullOrEmpty(state))
{
int parsed = 0;
if (int.TryParse(state, out parsed))
{
magicCode = parsed.ToString();
}
}
var tokenResponse = await(turnContext.Adapter as IUserTokenProvider).GetUserTokenAsync(turnContext, _connectionName, magicCode, cancellationToken: cancellationToken);
if (tokenResponse == null || string.IsNullOrEmpty(tokenResponse.Token))
{
// There is no token, so the user has not signed in yet.
// Retrieve the OAuth Sign in Link to use in the MessagingExtensionResult Suggested Actions
var signInLink = await(turnContext.Adapter as IUserTokenProvider).GetOauthSignInLinkAsync(turnContext, _connectionName, cancellationToken);
return new MessagingExtensionResponse
{
ComposeExtension = new MessagingExtensionResult
{
Type = "auth",
SuggestedActions = new MessagingExtensionSuggestedAction
{
Actions = new List<CardAction>
{
new CardAction
{
Type = ActionTypes.OpenUrl,
Value = signInLink,
Title = "Bot Service OAuth",
},
},
},
},
};
}
var heroCard = new ThumbnailCard
{
Title = "Thumbnail Card",
Text = query.Url,
Images = new List<CardImage> { new CardImage("https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png") },
};
var attachments = new MessagingExtensionAttachment(HeroCard.ContentType, null, heroCard);
var result = new MessagingExtensionResult("list", "result", new[] { attachments });
return new MessagingExtensionResponse(result);
}