我目前正在尝试使用带有ASP .NET MVC的Microsoft Graph API在用户Outlook日历上创建事件。不幸的是,创建事件的文档不包含示例。我发现并尝试修改发送电子邮件的示例(此处:https://github.com/microsoftgraph/aspnet-connect-rest-sample)。我能够将完全空白的初始事件发送到我的日历。当尝试发送事件对象本身时,我遇到了状态响应BAD REQUEST。如果有人能提供帮助,我们将不胜感激。
答案 0 :(得分:2)
有关如何构造Event对象的参考,您可以查看官方Microsoft Graph SDK如何构造此对象:请参阅GitHub上的最新源here。
有关在不使用SDK的情况下进行此REST调用的示例,您可以参考UserSnippets#CreateEventAsync()
- 下面粘贴了一段摘录。虽然下面的示例不使用本机对象进行序列化,但它希望能够传达它如何工作的本质。
HttpClient client = new HttpClient();
var token = await AuthenticationHelper.GetTokenHelperAsync();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
// Endpoint for the current user's events
Uri eventsEndpoint = new Uri(serviceEndpoint + "me/events");
// Build contents of post body and convert to StringContent object.
// Using line breaks for readability.
// Specifying the round-trip format specifier ("o") to the DateTimeOffset.ToString() method
// so that the datetime string can be converted into an Edm.DateTimeOffset object:
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Roundtrip
string postBody = "{'Subject':'Weekly Sync'," + "'Location':{'DisplayName':'Water Cooler'}," + "'Attendees':[{'Type':'Required','EmailAddress': {'Address':'mara@fabrikam.com'} }]," + "'Start': {'DateTime': '" + new DateTime(2014, 12, 1, 9, 30, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'End': {'DateTime': '" + new DateTime(2014, 12, 1, 10, 0, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'Body':{'Content': 'Status updates, blocking issues, and next steps.', 'ContentType':'Text'}}";
var createBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(eventsEndpoint, createBody);
if (response.IsSuccessStatusCode) {
string responseContent = await response.Content.ReadAsStringAsync();
jResult = JObject.Parse(responseContent);
createdEventId = (string) jResult["id"];
Debug.WriteLine("Created event: " + createdEventId);
} else {
// some appropriate error handling here
}
有关传输的JSON的示例:
{
"subject": "Weekly Sync",
"location": {
"displayName": "Water Cooler"
},
"attendees": [
{
"type": "Required",
"emailAddress": {
"address": "mara@fabrikam.com"
}
}
],
"start": {
"dateTime": "2016-02-02T17:45:00.0000000",
"timeZone": "UTC"
},
"end": {
"dateTime": "2016-02-02T18:00:00.0000000",
"timeZone": "UTC"
},
"body": {
"content": "Status updates, blocking issues,and nextsteps.",
"contentType": "Text"
}
}
其他帮助: 有一个有用的Graph Explorer Sandbox可供您在浏览器中测试请求