找不到用于链接展开的身份验证流程的良好示例。我设法使用this example运行oauth流。但是,在用户提供登录名和密码后,并且漫游器第二次命中OnTeamsAppBasedLinkQueryAsync
时,GetUserTokenAsync
仍返回null。所以我不遵循身份验证流程完成后从哪里获取令牌。我应该以某种方式坚持下去吗?团队是否会在每次请求时将令牌发送给我,或者令牌应该如何工作?
因此,在我的情况下,以下代码始终返回null:
var tokenResponse = await (turnContext.Adapter as IUserTokenProvider)
.GetUserTokenAsync(turnContext, _connectionName, default(string),
cancellationToken: cancellationToken);
答案 0 :(得分:1)
似乎AppBasedLinkQuery
上没有'state'字段。身份验证流程完成后,将再次调用OnTeamsAppBasedLinkQueryAsync
,并且turnContext.Activity.Value
将包含url和'state'(或魔术代码)。我们将把该字段添加到AppBasedLinkQuery
中(在这里创建一个问题:microsoft/botbuilder-dotnet#3429)。
一种解决方法是直接从Activity.Value
检索状态/魔术码,例如:
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);
}