【问题标题】:Cannot access images from Microsoft Teams Activity when added by clipboard通过剪贴板添加时无法从 Microsoft Teams Activity 访问图像
【发布时间】:2020-04-21 14:52:53
【问题描述】:

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

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

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

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

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

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

感谢您的宝贵时间!

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

我从这里使用 OnMessageActivityAsync 重载:
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"

【问题讨论】:

  • 嗨乔希!欢迎来到 SO。感谢您提出明确的问题。在这种情况下,代码示例可能会有所帮助。你能提供你的代码的sn-ps吗?
  • 您尝试从哪里访问图像,这是其他门户吗? 401 unauthorized 表示您的图片网址存在访问问题

标签: c# botframework microsoft-teams


【解决方案1】:

已回复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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多