添加附件
本文内容
命名空间:microsoft.graph
使用此 API 新建附件。
附件可以是下列类型之一:
所有这些类型的 attachment 资源均派生自 attachment 资源。
权限
要调用此 API,需要以下权限之一。要了解详细信息,包括如何选择权限的信息,请参阅权限 。
权限类型
权限(从最低特权到最高特权)
委派(工作或学校帐户)
Mail.ReadWrite
委派(个人 Microsoft 帐户)
Mail.ReadWrite
应用程序
Mail.ReadWrite
HTTP 请求
POST /me/messages/{id}/attachments
POST /users/{id | userPrincipalName}/messages/{id}/attachments
名称
类型
说明
Authorization
string
Bearer {token}。必需。
Content-Type
string
实体正文中的数据性质。必需。
请求正文
在请求正文中,提供 Attachment 对象的 JSON 表示形式。
响应
如果成功,此方法在响应正文中返回 201 Created 响应代码和 Attachment 对象。
示例(文件附件)
请求
下面是一个请求示例。
POST https://graph.microsoft.com/v1.0/me/messages/{id}/attachments
Content-type: application/json
{
"@odata.type": "microsoft.graph.fileAttachment",
"name": "name-value",
"contentType": "contentType-value",
"isInline": false,
"contentLocation": "contentLocation-value",
"contentBytes": "base64-contentBytes-value"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var attachment = new FileAttachment
{
Name = "name-value",
ContentType = "contentType-value",
IsInline = false,
ContentLocation = "contentLocation-value",
ContentBytes = Convert.FromBase64String("base64-contentBytes-value")
};
await graphClient.Me.Messages["{message-id}"].Attachments
.Request()
.AddAsync(attachment);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
const options = {
authProvider,
};
const client = Client.init(options);
const attachment = {
'@odata.type': 'microsoft.graph.fileAttachment',
name: 'name-value',
contentType: 'contentType-value',
isInline: false,
contentLocation: 'contentLocation-value',
contentBytes: 'base64-contentBytes-value'
};
await client.api('/me/messages/{id}/attachments')
.post(attachment);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/messages/{id}/attachments"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphAttachment *attachment = [[MSGraphAttachment alloc] init];
[attachment setName:@"name-value"];
[attachment setContentType:@"contentType-value"];
[attachment setIsInline: false];
[attachment setContentLocation:@"contentLocation-value"];
[attachment setContentBytes:@"base64-contentBytes-value"];
NSError *error;
NSData *attachmentData = [attachment getSerializedDataWithError:&error];
[urlRequest setHTTPBody:attachmentData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
FileAttachment attachment = new FileAttachment();
attachment.name = "name-value";
attachment.contentType = "contentType-value";
attachment.isInline = false;
attachment.contentLocation = "contentLocation-value";
attachment.contentBytes = Base64.getDecoder().decode("base64-contentBytes-value");
graphClient.me().messages("{id}").attachments()
.buildRequest()
.post(attachment);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewAttachment()
name := "name-value"
requestBody.SetName(&name)
contentType := "contentType-value"
requestBody.SetContentType(&contentType)
isInline := false
requestBody.SetIsInline(&isInline)
requestBody.SetAdditionalData(map[string]interface{}{
"@odata.type": "microsoft.graph.fileAttachment",
"contentLocation": "contentLocation-value",
"contentBytes": "base64-contentBytes-value",
}
messageId := "message-id"
result, err := graphClient.Me().MessagesById(&messageId).Attachments().Post(requestBody)
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
Import-Module Microsoft.Graph.Mail
$params = @{
"@odata.type" = "microsoft.graph.fileAttachment"
Name = "name-value"
ContentType = "contentType-value"
IsInline = $false
ContentLocation = "contentLocation-value"
ContentBytes = "base64-contentBytes-value"
}
# A UPN can also be used as -UserId.
New-MgUserMessageAttachment -UserId $userId -MessageId $messageId -BodyParameter $params
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
在请求正文中,提供 attachment 对象的 JSON 表示形式。
响应
这是一个示例响应。注意:为提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 201 Created
示例(项目附件)
请求
POST https://graph.microsoft.com/v1.0/me/events/{id}/attachments
Content-type: application/json
{
"@odata.type": "#Microsoft.OutlookServices.ItemAttachment",
"name": "name-value",
"item": {
"@odata.type": "microsoft.graph.message"
}
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var attachment = new ItemAttachment
{
Name = "name-value",
Item = new Message
{
}
};
await graphClient.Me.Events["{event-id}"].Attachments
.Request()
.AddAsync(attachment);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
const options = {
authProvider,
};
const client = Client.init(options);
const attachment = {
'@odata.type': '#Microsoft.OutlookServices.ItemAttachment',
name: 'name-value',
item: {
'@odata.type': 'microsoft.graph.message'
}
};
await client.api('/me/events/{id}/attachments')
.post(attachment);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/events/{id}/attachments"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphAttachment *attachment = [[MSGraphAttachment alloc] init];
[attachment setName:@"name-value"];
MSGraphOutlookItem *item = [[MSGraphOutlookItem alloc] init];
[attachment setItem:item];
NSError *error;
NSData *attachmentData = [attachment getSerializedDataWithError:&error];
[urlRequest setHTTPBody:attachmentData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
ItemAttachment attachment = new ItemAttachment();
attachment.name = "name-value";
Message item = new Message();
attachment.item = item;
graphClient.me().events("{id}").attachments()
.buildRequest()
.post(attachment);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewAttachment()
name := "name-value"
requestBody.SetName(&name)
requestBody.SetAdditionalData(map[string]interface{}{
"@odata.type": "#Microsoft.OutlookServices.ItemAttachment",
}
eventId := "event-id"
result, err := graphClient.Me().EventsById(&eventId).Attachments().Post(requestBody)
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
Import-Module Microsoft.Graph.Calendar
$params = @{
"@odata.type" = "#Microsoft.OutlookServices.ItemAttachment"
Name = "name-value"
Item = @{
"@odata.type" = "microsoft.graph.message"
}
}
# A UPN can also be used as -UserId.
New-MgUserEventAttachment -UserId $userId -EventId $eventId -BodyParameter $params
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
响应
下面是一个响应示例。注意:为了简单起见,可能会将此处所示的响应对象截断。将从实际调用中返回所有属性。
HTTP/1.1 201 Created