无法使用C#通过Microsoft Graph API在Microsoft Teams中发布内嵌图像以及消息

时间:2020-09-23 13:03:00

标签: c# microsoft-graph-api microsoft-teams microsoft-graph-sdks microsoft-graph-teams

我一直在尝试使用C#通过MS Graph API在Microsoft Teams中发布图像以及消息,但是无法这样做。

下面是我尝试过的代码:

_document.js

正在上传图片,但未显示任何内容,即图片已损坏/损坏,如下所示:

enter image description here

我什至尝试使用以下代码将图像添加到已发送的消息中,但会引发“未知错误”异常。

getInitialProps

我可以使用下面的URL和有效负载,使用MS Graph资源管理器中的Post HTTP方法成功地将同一图像和文本一起发布。

string userName = ConfigurationManager.AppSettings["UserName"]; string password = ConfigurationManager.AppSettings["Password"]; System.Security.SecureString passWordSecureString = new System.Security.SecureString(); foreach (char c in password.ToCharArray()) passWordSecureString.AppendChar(c); var clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; var tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; string[] scopes = { "ChannelMessage.Send", "Group.ReadWrite.All", "User.Read" }; IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder .Create(clientId) .WithTenantId(tenantId) .Build(); //creating the graph user context. UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes); var chatMessage = new ChatMessage { Subject = null, Body = new ItemBody { ContentType = BodyType.Html, Content = "Hello world </br><div><div>\n<div><span><img height=\"297\" src=\"../hostedContents/1/$value\" width=\"297\" style=\"vertical-align:bottom; width:297px; height:297px\"></span>\n\n</div>\n\n\n</div>\n</div>" }, }; ChatMessageHostedContent chatMessageHostedContent = new ChatMessageHostedContent { ContentBytes = Encoding.ASCII.GetBytes("iVBORw0KGgoAAAANSUhEUgAAASkA..."), ContentType = "image/png", AdditionalData = new Dictionary<string, object>() { {"@microsoft.graph.temporaryId", "1"} } }; IChatMessageHostedContentsCollectionPage chatMessageHostedContentsCollectionPage = new ChatMessageHostedContentsCollectionPage(); chatMessageHostedContentsCollectionPage.Add(chatMessageHostedContent); if (chatMessageHostedContentsCollectionPage.Count > 0) chatMessage.HostedContents = chatMessageHostedContentsCollectionPage; ChatMessage sentMessage = await graphClient.Teams["{id}"].Channels["{id}"].Messages .Request() .AddAsync(chatMessage);

await graphClient.Teams["{id}"].Channels["{id}"].Messages[sentMessage.Id].HostedContents
        .Request()
        .AddAsync(chatMessageHostedContent);

有人可以建议代码中有什么问题吗?

0 个答案:

没有答案