使用 Microsoft Graph 客户端创建日历事件
Create calendar event using Microsoft Graph Client
我正在尝试了解如何使用 Microsoft Graph JavaScript 客户端创建日历事件。
我已经设法检索到必要的 accessToken
并且可以与 API 交互(即检索事件、日历、前 10 封电子邮件),但我不确定如何使用 API 创建事件。
client
.api('/me/events')
.header('X-AnchorMailbox', emailAddress)
我是否使用 post 发送事件 json 对象?
我建议查看 Read Medocumentation 以了解有关如何使用此库的详细信息。
要回答您的问题,您需要创建一个 Event 对象。例如:
var event = {
"subject": "Let's go for lunch",
"body": {
"contentType": "HTML",
"content": "Does late morning work for you?"
},
"start": {
"dateTime": "2017-04-15T12:00:00",
"timeZone": "Pacific Standard Time"
},
"end": {
"dateTime": "2017-04-15T14:00:00",
"timeZone": "Pacific Standard Time"
},
"location": {
"displayName": "Harry's Bar"
},
"attendees": [{
"emailAddress": {
"address": "samanthab@contoso.onmicrosoft.com",
"name": "Samantha Booth"
},
"type": "required"
}]
}
然后您需要 .post
此对象到 /events
端点:
client
.api('/me/events')
.post(event, (err, res) => {
console.log(res)
})
我使用 Microsoft Graph API for C#.Net MVC 在 Outlook 日历中创建了一个事件,如下所示。
我相信无论谁将阅读此答案,都已经在 https://apps.dev.microsoft.com 上创建了一个应用程序,并拥有用于此应用程序的凭据。
请按照本教程 How to use Outlook REST APIs 进行初始项目和 OAuth 设置。本文还介绍了如何创建如上所述的应用程序。
现在为了编码,我做了以下事情。
创建 class 来保存事件属性。
public class ToOutlookCalendar
{
public ToOutlookCalendar()
{
Attendees = new List<Attendee>();
}
[JsonProperty("subject")]
public string Subject { get; set; }
[JsonProperty("body")]
public Body Body { get; set; }
[JsonProperty("start")]
public End Start { get; set; }
[JsonProperty("end")]
public End End { get; set; }
[JsonProperty("attendees")]
public List<Attendee> Attendees { get; set; }
[JsonProperty("location")]
public LocationName Location { get; set; }
}
public class Attendee
{
[JsonProperty("emailAddress")]
public EmailAddress EmailAddress { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public class EmailAddress
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Body
{
[JsonProperty("contentType")]
public string ContentType { get; set; }
[JsonProperty("content")]
public string Content { get; set; }
}
public class LocationName
{
[JsonProperty("displayName")]
public string DisplayName { get; set; }
}
public class End
{
[JsonProperty("dateTime")]
public string DateTime { get; set; }
[JsonProperty("timeZone")]
public string TimeZone { get; set; }
}
在我的控制器(您将在上面 url 中提到的用于项目设置的控制器)中,我创建了一个用于创建事件的操作方法,如下所示:
public async Task<ActionResult> CreateOutlookEvent()
{
string token = await GetAccessToken(); //this will be created in the project setup url above
if (string.IsNullOrEmpty(token))
{
// If there's no token in the session, redirect to Home
return Redirect("/");
}
using (HttpClient c = new HttpClient())
{
string url = "https://graph.microsoft.com/v1.0/me/events";
//with your properties from above except for "Token"
ToOutlookCalendar toOutlookCalendar = CreateObject();
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(toOutlookCalendar), Encoding.UTF8, "application/json");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = httpContent;
//Authentication token
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await c.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
}
return null;
}
创建虚拟事件对象的 CreateObject 方法(请原谅我的命名约定,但这只是为了演示目的)
public static ToOutlookCalendar CreateObject()
{
ToOutlookCalendar toOutlookCalendar = new ToOutlookCalendar
{
Subject = "Code test",
Body = new Body
{
ContentType = "HTML",
Content = "Testing outlook service"
},
Start = new End
{
DateTime = "2018-11-30T12:00:00",
TimeZone = "Pacific Standard Time"
},
End = new End
{
DateTime = "2018-11-30T15:00:00",
TimeZone = "Pacific Standard Time"
},
Location = new LocationName
{
DisplayName = "Harry's Bar"
}
};
return toOutlookCalendar;
}
并且我能够在 Outlook 日历中创建一个事件。此答案的某些部分改编自此 。
希望对大家有所帮助。
我正在尝试了解如何使用 Microsoft Graph JavaScript 客户端创建日历事件。
我已经设法检索到必要的 accessToken
并且可以与 API 交互(即检索事件、日历、前 10 封电子邮件),但我不确定如何使用 API 创建事件。
client
.api('/me/events')
.header('X-AnchorMailbox', emailAddress)
我是否使用 post 发送事件 json 对象?
我建议查看 Read Medocumentation 以了解有关如何使用此库的详细信息。
要回答您的问题,您需要创建一个 Event 对象。例如:
var event = {
"subject": "Let's go for lunch",
"body": {
"contentType": "HTML",
"content": "Does late morning work for you?"
},
"start": {
"dateTime": "2017-04-15T12:00:00",
"timeZone": "Pacific Standard Time"
},
"end": {
"dateTime": "2017-04-15T14:00:00",
"timeZone": "Pacific Standard Time"
},
"location": {
"displayName": "Harry's Bar"
},
"attendees": [{
"emailAddress": {
"address": "samanthab@contoso.onmicrosoft.com",
"name": "Samantha Booth"
},
"type": "required"
}]
}
然后您需要 .post
此对象到 /events
端点:
client
.api('/me/events')
.post(event, (err, res) => {
console.log(res)
})
我使用 Microsoft Graph API for C#.Net MVC 在 Outlook 日历中创建了一个事件,如下所示。 我相信无论谁将阅读此答案,都已经在 https://apps.dev.microsoft.com 上创建了一个应用程序,并拥有用于此应用程序的凭据。
请按照本教程 How to use Outlook REST APIs 进行初始项目和 OAuth 设置。本文还介绍了如何创建如上所述的应用程序。
现在为了编码,我做了以下事情。
创建 class 来保存事件属性。
public class ToOutlookCalendar
{
public ToOutlookCalendar()
{
Attendees = new List<Attendee>();
}
[JsonProperty("subject")]
public string Subject { get; set; }
[JsonProperty("body")]
public Body Body { get; set; }
[JsonProperty("start")]
public End Start { get; set; }
[JsonProperty("end")]
public End End { get; set; }
[JsonProperty("attendees")]
public List<Attendee> Attendees { get; set; }
[JsonProperty("location")]
public LocationName Location { get; set; }
}
public class Attendee
{
[JsonProperty("emailAddress")]
public EmailAddress EmailAddress { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public class EmailAddress
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Body
{
[JsonProperty("contentType")]
public string ContentType { get; set; }
[JsonProperty("content")]
public string Content { get; set; }
}
public class LocationName
{
[JsonProperty("displayName")]
public string DisplayName { get; set; }
}
public class End
{
[JsonProperty("dateTime")]
public string DateTime { get; set; }
[JsonProperty("timeZone")]
public string TimeZone { get; set; }
}
在我的控制器(您将在上面 url 中提到的用于项目设置的控制器)中,我创建了一个用于创建事件的操作方法,如下所示:
public async Task<ActionResult> CreateOutlookEvent()
{
string token = await GetAccessToken(); //this will be created in the project setup url above
if (string.IsNullOrEmpty(token))
{
// If there's no token in the session, redirect to Home
return Redirect("/");
}
using (HttpClient c = new HttpClient())
{
string url = "https://graph.microsoft.com/v1.0/me/events";
//with your properties from above except for "Token"
ToOutlookCalendar toOutlookCalendar = CreateObject();
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(toOutlookCalendar), Encoding.UTF8, "application/json");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = httpContent;
//Authentication token
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await c.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
}
return null;
}
创建虚拟事件对象的 CreateObject 方法(请原谅我的命名约定,但这只是为了演示目的)
public static ToOutlookCalendar CreateObject()
{
ToOutlookCalendar toOutlookCalendar = new ToOutlookCalendar
{
Subject = "Code test",
Body = new Body
{
ContentType = "HTML",
Content = "Testing outlook service"
},
Start = new End
{
DateTime = "2018-11-30T12:00:00",
TimeZone = "Pacific Standard Time"
},
End = new End
{
DateTime = "2018-11-30T15:00:00",
TimeZone = "Pacific Standard Time"
},
Location = new LocationName
{
DisplayName = "Harry's Bar"
}
};
return toOutlookCalendar;
}
并且我能够在 Outlook 日历中创建一个事件。此答案的某些部分改编自此
希望对大家有所帮助。