通过剪贴板添加时无法访问来自 Microsoft Teams Activity 的图像

Cannot access images from Microsoft Teams Activity when added by clipboard

我正在开发一个机器人,它通过 Botframework 从 Teams 接收消息并将它们发送到另一个应用程序。 我正在使用 .NET Core。

到目前为止一切正常,我可以移动文本、表情符号、文件等。 但是剪贴板中的图像有问题。

发送普通文件时,我可以使用从 Botframework 收到的 activity 的 Attachment.Content["downloadUrl"] 访问它。

但是当我从剪贴板插入图像时,它以另一种方式存储,使用 Activity 我得到两个具有不同 url 的附件(我想一个用于布局,一个用于图像资源?) ,如下图所示,但是在尝试获取图像时,我收到 401 Unauthorized。

有没有办法访问从剪贴板粘贴的这些图像,或者这些图像可能只能从 Microsoft Teams 中 visible/accessible 访问?

欧比旺克诺比,帮帮我。你是我唯一的希望。

感谢您的宝贵时间!

编辑:感谢您的热烈欢迎!

我正在使用来自此处的 OnMessageActivity异步重载:
https://docs.microsoft.com/en-us/microsoftteams/platform/bots/bot-basics?tabs=csharp#bot-logic

    public class BotActivityHandler : Microsoft.Bot.Builder.Teams.TeamsActivityHandler
    {
        [...]

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // it's either this url or the one parsed from the html, but both receive a 401 with a GET request like below or with Postman
            var imageUrl = turnContext.Activity.Attachments.Where(x => !x.ContentUrl.IsNullOrEmpty()).ToList().First().ContentUrl;

            var response = await myHttpClient.GetAsync(imageUrl);
            var img = await response.Content.ReadAsStreamAsync();
        }
[...]

turnContext 包含一个带有两个附件的 Activity: 一个有类似

的内容
<div><div>\n<div><span><img
src=\"https://eu-api.asm.skype.com/v1/objects/[...]/views/imgo\"
 width=\"247\" id=\"[...]\" itemscope=\"\" itemtype=\"http://schema.skype.com/AMSImage\"\"></span>\n\n</div>\n\n\n</div>\n</div>

还有一个像 ContentUrl

"https://smba.trafficmanager.net/emea/v3/attachments/[...]/views/original"

已在 https://github.com/MicrosoftDocs/msteams-docs/issues/1561

中回答

可以使用 MicrosoftAppCredentials 接收 JWT 令牌来访问图像。

var token = await new MicrosoftAppCredentials("appId", "password").GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);