2017-04-12 126 views
0

活動我試圖把這個代碼O365創建使用REST API

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的應用程序中訪問的。

創建日曆事件的最佳方式是什麼?

回答

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