首页 > 解决方案 > 如何获取 MSTeam 聊天附件的下载 URL?

问题描述

我可以使用以下测试版 API https://graph.microsoft.com/beta/teams/{group-id-for-teams}/channels/{channel-id}/messages获取 MSTeam 聊天的附件信息/{message-id}/replies/{reply-message-id}

例如https://graph.microsoft.com/beta/teams/e7884ec9-cbf3-4661-b74a-6fc42ac34ce1/channels/19:6a04c17090e249319b3731cbe72e6085@thread.tacv2/messages/1600060770982/replies/1600061187 _

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#teams('e7884ec9-cbf3-4661-b74a-6fc42ac34ce1')/channels('19%3A6a04c17090e249319b3731cbe72e6085%40thread.tacv2')/messages('1600060770982')/replies/$entity",
    "id": "1600061187436",
    "replyToId": "1600060770982",
    "etag": "1600061187436",
.....
    "attachments": [
        {
            "id": "40ce4e84-6e47-43ed-acb9-9edcddff5ef4",
            "contentType": "reference",
            "contentUrl": "https://.......1/Shared Documents/General/MsteamchatHistory Documentation.docx",
            "content": null,
            "name": "MsteamchatHistory Documentation.docx",
            "thumbnailUrl": null
        }
    ],
    ......
}

无法使用 ContentUrl 下载,因此找到了获取 downloadURL 的方法,以便可以使用 HttpWebRequest 下载它。

获取下载 URL 的一种方法是使用 https://graph.microsoft.com/v1.0/groups/{group-id-for-teams}/drive/items/{item-id}

例如https://graph.microsoft.com/v1.0/groups/e7884ec9-cbf3-4661-b74a-6fc42ac34ce1/drive/items/012PWSBOUEJ3HEAR3O5VB2ZOM63TO76XXU

这使

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups('e7884ec9-cbf3-4661-b74a-6fc42ac34ce1')/drive/items/$entity",
    "@microsoft.graph.downloadUrl": "https://nepa1.sharepoint.com/sites/Venio1/_layouts/15/download.aspx?UniqueId=40ce....0&ApiVersion=2.0",
    "createdDateTime": "2020-09-14T05:26:14Z",
    "eTag": "\"{40CE4E84-6E47-43ED-ACB9-9EDCDDFF5EF4},2\"",
    "id": "012PWSBOUEJ3HEAR3O5VB2ZOM63TO76XXU",
    "lastModifiedDateTime": "2020-09-14T05:26:21Z",
    "name": "MsteamchatHistory Documentation.docx",
    "webUrl": "https://nepa1.sharepoint.com/sites/Venio1/_layouts/15/Doc.aspx?sourcedoc=%7B40CE4E84-6E47-43ED-ACB9-9EDCDDFF5EF4%7D&file=MsteamchatHistory%20Documentation.docx&action=default&mobileredirect=true",
    "cTag": "\"c:{40CE4E84-6E47-43ED-ACB9-9EDCDDFF5EF4},3\"",
    "size": 563211,
    .....
}

在这里,我通过探索组找到了 item-id。但我想从相应的附件 ID 中找到项目 ID,以便我可以获取该附件的下载 URL,并使用它使用 HttpWebRequest 进行下载。

那么,是否可以从附件 ID 中获取驱动器项目 ID?如果是,怎么做?

提前致谢。

标签: downloadmicrosoft-graph-apioffice365attachmentmicrosoft-graph-teams

解决方案


我的一个客户最近问过我这个问题,我实际上找到了一个快捷方式来为 SharePoint 文件创建下载链接。由于您的团队附件存储在 SharePoint 文件夹中(根据 URL),您应该也可以使用此技巧。

使用内容 URL,将&download=1, ?download=1, 或添加?Web=0到 URL 的末尾,如下所示:

https://.......1/Shared Documents/General/MsteamchatHistory Documentation.docx?download=1

并且链接变成了直接下载链接!

我还没有找到任何信息说明为什么会这样以及为什么有时一个结局会起作用而其他结局不会,但到目前为止,至少一个结局对我测试过的所有文件都有效。


推荐阅读