创建 taskFileAttachment
本文内容
命名空间:microsoft.graph
重要
Microsoft Graph /beta
版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
将新的 taskFileAttachment 对象添加到 todoTask 。
此操作限制可添加到 3 MB 以下的附件的大小。 如果文件附件的大小超过 3 MB, 请创建上传会话 来上传附件。
此 API 可用于以下国家级云部署 。
全局服务
美国政府 L4
美国政府 L5 (DOD)
由世纪互联运营的中国
✅
✅
✅
❌
权限
为此 API 选择标记为最低特权的权限。 只有在应用需要它时 ,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型 。 要了解有关这些权限的详细信息,请参阅 权限参考 。
权限类型
最低特权权限
更高特权权限
委派(工作或学校帐户)
Tasks.ReadWrite
不可用。
委派(个人 Microsoft 帐户)
Tasks.ReadWrite
不可用。
应用程序
不支持。
不支持。
HTTP 请求
POST /me/todo/lists/{id}/tasks/{id}/attachments
POST /users/{id}/todo/lists/{id}/tasks/{id}/attachments
名称
说明
Authorization
持有者 {token}。 必填。 详细了解 身份验证和授权 。
Content-Type
application/json. 必需。
请求正文
在请求正文中,提供 taskFileAttachment 对象的 JSON 表示形式。
创建文件附件时,包括 "@odata.type": "#microsoft.graph.taskFileAttachment"
和 所需的属性。
属性
类型
说明
contentBytes
Binary
文件的 Base64 编码内容。 必填。
contentType
String
附件的内容类型。
name
String
表示嵌入附件的图标下显示的文本的名称。 这不必是实际的文件名。 必填。
size
Int32
附件大小,以字节为单位。
响应
如果成功,此方法在 201 Created
响应正文中返回响应代码和 taskFileAttachment 对象。
示例
请求
以下示例显示了一个请求。
POST https://graph.microsoft.com/beta/me/todo/lists/AAMkpsDRVK=/tasks/AAKdfjhgsjhgJ=/attachments
Content-Type: application/json
{
"@odata.type": "#microsoft.graph.taskFileAttachment",
"name": "smile",
"contentBytes": "a0b1c76de9f7=",
"contentType": "image/gif"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new TaskFileAttachment
{
OdataType = "#microsoft.graph.taskFileAttachment",
Name = "smile",
ContentBytes = Convert.FromBase64String("a0b1c76de9f7="),
ContentType = "image/gif",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Todo.Lists["{todoTaskList-id}"].Tasks["{todoTask-id}"].Attachments.PostAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc-beta users todo lists tasks attachments create --user-id {user-id} --todo-task-list-id {todoTaskList-id} --todo-task-id {todoTask-id} --body '{\
"@odata.type": "#microsoft.graph.taskFileAttachment",\
"name": "smile",\
"contentBytes": "a0b1c76de9f7=",\
"contentType": "image/gif"\
}\
'
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestBody := graphmodels.NewAttachmentBase()
name := "smile"
requestBody.SetName(&name)
contentBytes := []byte("a0b1c76de9f7=")
requestBody.SetContentBytes(&contentBytes)
contentType := "image/gif"
requestBody.SetContentType(&contentType)
attachments, err := graphClient.Me().Todo().Lists().ByTodoTaskListId("todoTaskList-id").Tasks().ByTodoTaskId("todoTask-id").Attachments().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
TaskFileAttachment attachmentBase = new TaskFileAttachment();
attachmentBase.setOdataType("#microsoft.graph.taskFileAttachment");
attachmentBase.setName("smile");
byte[] contentBytes = Base64.getDecoder().decode("a0b1c76de9f7=");
attachmentBase.setContentBytes(contentBytes);
attachmentBase.setContentType("image/gif");
AttachmentBase result = graphClient.me().todo().lists().byTodoTaskListId("{todoTaskList-id}").tasks().byTodoTaskId("{todoTask-id}").attachments().post(attachmentBase);
const options = {
authProvider,
};
const client = Client.init(options);
const attachmentBase = {
'@odata.type': '#microsoft.graph.taskFileAttachment',
name: 'smile',
contentBytes: 'a0b1c76de9f7=',
contentType: 'image/gif'
};
await client.api('/me/todo/lists/AAMkpsDRVK=/tasks/AAKdfjhgsjhgJ=/attachments')
.version('beta')
.post(attachmentBase);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\TaskFileAttachment;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new TaskFileAttachment();
$requestBody->setOdataType('#microsoft.graph.taskFileAttachment');
$requestBody->setName('smile');
$requestBody->setContentBytes(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('a0b1c76de9f7=')));
$requestBody->setContentType('image/gif');
$result = $graphServiceClient->me()->todo()->lists()->byTodoTaskListId('todoTaskList-id')->tasks()->byTodoTaskId('todoTask-id')->attachments()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Users
$params = @{
"@odata.type" = "#microsoft.graph.taskFileAttachment"
name = "smile"
contentBytes = "a0b1c76de9f7="
contentType = "image/gif"
}
# A UPN can also be used as -UserId.
New-MgBetaUserTodoListTaskAttachment -UserId $userId -TodoTaskListId $todoTaskListId -TodoTaskId $todoTaskId -BodyParameter $params
from msgraph import GraphServiceClient
from msgraph.generated.models.task_file_attachment import TaskFileAttachment
graph_client = GraphServiceClient(credentials, scopes)
request_body = TaskFileAttachment(
odata_type = "#microsoft.graph.taskFileAttachment",
name = "smile",
content_bytes = base64.urlsafe_b64decode("a0b1c76de9f7="),
content_type = "image/gif",
)
result = await graph_client.me.todo.lists.by_todo_task_list_id('todoTaskList-id').tasks.by_todo_task_id('todoTask-id').attachments.post(request_body)
响应
以下示例显示了相应的响应。
注意: 为了提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 201 Created
Content-Type: application/json
{
"@odata.type": "#microsoft.graph.taskFileAttachment",
"id": "AAMkADNkN2R",
"lastModifiedDateTime": "2017-01-26T08:48:28Z",
"name": "smile",
"contentType": "image/gif",
"size": 1008
}