O365使用REST API创建事件

时间:2017-04-12 15:54:20

标签: c# rest office365

我正在尝试调用此代码

 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);
         }
 }

创建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);

  } 

我收到错误请求作为回复。有没有替代建立一个事件?我希望这个代码非常薄,因为这可以在非基于UI的应用程序中访问

创建日历活动的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

此修复涉及在开始和结束中设置时区

   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);