在Microsoft团队频道中以消息形式从本地系统发送文件/图像



如何使用Microsoft Graph API的发送消息在Microsoft Teams Channel中发送本地文件/图像?

我遵循了一份文件https://learn.microsoft.com/en-us/graph/api/resources/chatmessageattachment?view=graph-rest-1.0在创建发送团队频道的消息时附加图像。我正在尝试发送图像的base64格式。但是我犯了一个错误。

{
"error": {
"code": "InternalServerError",
"message": "Failed to process request.",
"innerError": {
"date": "2020-09-25T11:43:02",
"request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"client-request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
}
}

似乎并没有合适的API让您直接将本地文件作为消息发送。

Creating chatMessage in a channel只支持发送已经在SharePoint中的文件。因此,发送本地文件需要两个步骤:首先,将文件上传到SharePoint,然后创建chatMessage。


1.将文件上载到SharePoint:

小于4MB:的文件

PUT https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/content
Header:
"Authorization" : "Bearer <access-token>"
Body: binary (select binary option in body in postman)
Upload a file using select file option
file-name: is file name along with extension example: test.txt

大于4MB:的文件

GET: https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/createUploadSession
Header:
"Authorization" : "Bearer <access-token>"

Get请求将返回上传url。

PUT <upload url>
Header:
"Authorization" : "Bearer <access-token>"
Body: binary (select binary option in body in postman)

有关更多详细信息,请参阅此处。

2.创建聊天消息:

POST https://graph.microsoft.com/v1.0/teams/{id}/channels/{id}/messages
Content-type: application/json
{
"body": {
"contentType": "html",
"content": "Here's the latest budget. <attachment id="153fa47d-18c9-4179-be08-9879815a9f90"></attachment>"
},
"attachments": [
{
"id": "153fa47d-18c9-4179-be08-9879815a9f90",
"contentType": "reference",
"contentUrl": "https://m365x987948.sharepoint.com/sites/test/Shared%20Documents/General/test%20doc.docx",
"name": "Budget.docx"
}
]
}

最新更新