我正在尝试弄清楚如何使用Microsoft Graph JavaScript Client创建日历事件。
我已设法检索必要的accessToken
并可以与API交互(即检索事件,日历,前10个电子邮件),但我不确定如何使用API创建一个事件。
client
.api('/me/events')
.header('X-AnchorMailbox', emailAddress)
我是否使用post发送事件json对象?
答案 0 :(得分:4)
我建议您查看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)
})
答案 1 :(得分:0)
如下所示,我使用Microsoft Graph API for C#.Net MVC在Outlook日历中创建了一个事件。 我相信将要阅读此答案的任何人都已经在https://apps.dev.microsoft.com上创建了一个应用程序,并且拥有在此应用程序中使用的凭据。
请按照本教程How to use Outlook REST APIs进行初始项目和OAuth设置。本文还介绍了如何如上所述创建应用。
现在我进行编码。
创建将保留事件属性的类。
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日历中创建一个事件。此答案的某些部分改编自THREAD。
希望它可以帮助某人。