简体   繁体   English

O365使用REST API创建事件

[英]O365 Create Events using REST API

I am trying to call this code 我正在尝试调用此代码

 string accessToken = @".."; //valid token with right scopes
 public string EventsUrl = @"https://outlook.office.com/api/v2.0/me/events";

 // generate body
 var postBody = JsonBody(invite);

 using (var client = new HttpClient())
 {
       using (var request = new HttpRequestMessage(HttpMethod.Post, EventsUrl))
        {
             request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
             var content = new StringContent(postBody, Encoding.UTF8, "application/json");

             request.Content = content;

             var response = await client.SendAsync(request);
             return (response.IsSuccessStatusCode);
         }
 }

The method that create JsonBody is, 创建JsonBody的方法是,

public string JsonBody(User user, Session session){

        var invite = new EventInvite
        {
            Attendees = new Attendee[1]
        };

        invite.Attendees[0] = new Attendee
        {
            Type = "Required",
            EmailAddress = new Emailaddress { Name = user.GetName(), Address = user.GetEmail() }
        };
        invite.Start = new Start { DateTime = session.DateTime_Start };
        invite.End = new End { DateTime = session.DateTime_Start.AddMinutes(15) };
        invite.Subject = session.Name;
        invite.Body = new Body { ContentType = "HTML", Content = $"Some Content" };

        return JsonConvert.SerializeObject(eventInvite);

  } 

I am getting a Bad Request as response. 我收到一个错误的请求作为回应。 Is there any alternative to build an Event? 是否有其他方法可以建立活动? I want this code to be very thin as this is accessed in non UI based application 我希望这段代码很薄,因为可以在非基于UI的应用程序中访问它

What is the best way to create Calendar Event? 创建日历事件的最佳方法是什么?

The fix involves setting up the Timezone in the Start and End 该修复程序涉及在“开始”和“结束”中设置时区

   string timeZone="Singapore Standard Time";
   invite.Start = new Start { DateTime = session.DateTime_Start, TimeZone = timeZone };
   invite.End = new End { DateTime = session.DateTime_Start.AddMinutes(60), TimeZone = timeZone };
   var postBody = JsonConvert.SerializeObject(invite, Formatting.Indented);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM