[英]How To Add Calendar Events to Outlook 365 Using C# MSGraph
我正在使用的当前应用程序实质上是为Microsoft Outlook 365创建包装器应用程序。我与MSGraph的合作不多,并且一直很难从“表单窗口”中将事件设置为登录用户的日历,或者在获取令牌后真正做任何事情。 这是我当前正在尝试使用的代码。
PublicClientApplication clientApp;
GraphServiceClient graphClient;
string token;
string userEmail;
public async void Start()
{
await GetDataAsync();
return;
}
async Task GetDataAsync()
{
clientApp = new PublicClientApplication(ConfigurationManager.AppSettings["ClientId"].ToString());
graphClient = new GraphServiceClient(
"https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", await GetTokenAsync(clientApp));
}));
var currentUser = await graphClient.Me.Request().GetAsync().ConfigureAwait(false);
userEmail = currentUser.Mail;
return;
}
async Task<string> GetTokenAsync(PublicClientApplication clientApp)
{
//need to pass scope of activity to get token
string[] Scopes = { "User.Read", "Calendars.ReadWrite", "Calendars.ReadWrite.Shared"};
token = null;
AuthenticationResult authResult = await clientApp.AcquireTokenAsync(Scopes).ConfigureAwait(false);
token = authResult.AccessToken;
return token;
}
public async void SetAppointment(string subject, DateTime start, DateTime end, List<Attendee> attendies)
{
try
{
using (HttpClient c = new HttpClient())
{
String requestURI = "https://graph.microsoft.com/v1.0/users/" + userEmail + "/calendar/events.";
ToOutlookCalendar toOutlookCalendar = new ToOutlookCalendar();
TimeZone Timezone = TimeZone.CurrentTimeZone;
toOutlookCalendar.Subject = subject;
toOutlookCalendar.Start = start;
toOutlookCalendar.End = end;
toOutlookCalendar.Attendees = attendies;
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(toOutlookCalendar), Encoding.UTF8, "application/json");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestURI);
request.Content = httpContent;
//Authentication token
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await c.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
UPDATE
好的,感谢下面的Seiya Su,我设法使这段代码大部分都起作用了,但是,每当我向日历中添加一个事件时,都需要登录。
public async void SetAppointment(string Subject, string Body, string Start, string End, string Location, List<string> attendees)
{
var myEvent = new Microsoft.Graph.Event();
myEvent.Subject = Subject;
myEvent.Body = new ItemBody() { ContentType = BodyType.Text, Content = Body };
myEvent.Start = new DateTimeTimeZone() { DateTime = Start, TimeZone = "" };
myEvent.End = new DateTimeTimeZone() { DateTime = End, TimeZone = "" };
myEvent.Location = new Location() { DisplayName = Location };
var appointment = await graphClient.Me.Calendar.Events.Request().AddAsync(myEvent);
我构建了一个测试方法来解决这个问题,并尝试了以下操作:
public async void graphTesting()
{
var myEvent = new Microsoft.Graph.Event();
myEvent.Subject = "Test";
myEvent.Body = new ItemBody() { ContentType = BodyType.Text, Content = "This is test." };
myEvent.Start = new DateTimeTimeZone() { DateTime = "2018-10-3T12:00:00", TimeZone = "Pacific Standard Time" };
myEvent.End = new DateTimeTimeZone() { DateTime = "2018-10-3T13:00:00", TimeZone = "Pacific Standard Time" };
myEvent.Location = new Location() { DisplayName = "conf room 1" };
//myEvent.Attendees = attendies;
var myEvent2 = new Microsoft.Graph.Event();
myEvent2.Subject = "Test";
myEvent2.Body = new ItemBody() { ContentType = BodyType.Text, Content = "This is test." };
myEvent2.Start = new DateTimeTimeZone() { DateTime = "2018-10-4T12:00:00", TimeZone = "Pacific Standard Time" };
myEvent2.End = new DateTimeTimeZone() { DateTime = "2018-10-4T13:00:00", TimeZone = "Pacific Standard Time" };
myEvent2.Location = new Location() { DisplayName = "conf room 1" };
//myEvent.Attendees = attendies;
// Create the event.
var user = graphClient.Me.Calendar.Events.Request();
await user.Header("bearer", token).AddAsync(myEvent);
await user.Header("bearer", token).AddAsync(myEvent2);
}
但是它仍然无法正常工作,要求我为添加的每个事件登录。 我同意用户必须登录,只要他们只登录一次,是否有人知道解决此问题的方法? 我目前的想法是将令牌传递给请求,但这似乎无济于事。
1您在代码中使用了错误的EndPoint。
2由于您可以使用MS图形库,因此这里有一个更有效的工作代码。您可以重新封装它。
var myEvent = new Microsoft.Graph.Event();
myEvent.Subject = "Test";
myEvent.Body = new ItemBody() { ContentType = BodyType.Text, Content = "This is test." };
myEvent.Start = new DateTimeTimeZone() { DateTime = "2018-9-29T12:00:00", TimeZone = "Pacific Standard Time" };
myEvent.End = new DateTimeTimeZone() { DateTime = "2018-9-29T13:00:00", TimeZone = "Pacific Standard Time" };
myEvent.Location = new Location() { DisplayName = "conf room 1" };
//myEvent.Attendees = attendies;
// Create the event.
//graphClient.Me.Calendar.Events.Request().AddAsync(myEvent)
var mySyncdEvent = await graphClient.Users["you mail"].Calendar.Events.Request()
.AddAsync(myEvent);
更新2018-10-5
对于您的更新代码,我们不需要每次都传递令牌。 我不确定您的From Windows是WPF还是Winform之类的。代码和身份验证流对于MVC项目是否应该工作得更好。 只需尝试将脱机或此类范围传递到您的GraphScope设置文件。
持续。 不要总是将遇到的新问题更新为现有问题。 尽管它们是相关的,但不再是一个问题。 虽然旧问题已经解决,但最好还是再提出一个新问题。 否则它们仍然是一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.