如何在没有 turncontext 的情况下获取特定用户 ID 和连接名称的令牌

How to get a token for specific userid and connectionname without turncontext

我正在后台任务中做一些工作,并且有可用的用户 ID(从 activity.from.id 中提取)和连接名称。

如何从机器人服务获取用户令牌? - turncontext 不可用。

请记住,Bot Framework REST API 调用必须经过授权,因此除了用户 ID 和连接名称之外,您还需要机器人的应用凭据。如果你拥有所有这些,你可以获得这样的用户令牌:

private async Task<string> GetUserTokenAsync(string userId, string connectionName, AppCredentials appCredentials)
{
    var client = new OAuthClient(new Uri(OAuthClientConfig.OAuthEndpoint), appCredentials);
    var response = await client.UserToken.GetTokenAsync(userId, connectionName);

    return response.Token;
}

这在某种程度上取自 BotFrameworkAdapter.cs 中的代码,但已修改为不需要转弯上下文。