Post events to Microsoft Graph c#

后端 未结 1 773
生来不讨喜
生来不讨喜 2021-01-16 09:23

sorry for the multiple posts, I can\'t get a post request to Microsoft Graph to work, I\'m following this documentation, I\'m trying to create an event in

https://g

相关标签:
1条回答
  • 2021-01-16 09:51

    To make this a little shorter/summarize this is what i use to create an event and is necessary.

    using (HttpClient c = new HttpClient())
    {
         String requestURI = "https://graph.microsoft.com/v1.0/users/"+userEmail+"/calendar/events.";
    
         //with your properties from above except for "Token"
         ToOutlookCalendar toOutlookCalendar = new ToOutlookCalendar();
    
         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();
    }
    

    You don't need these headers:

    htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com");
    htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    

    And Token is not Part of an Event object so you should remove it from ToOutlookCalendar.

    If you don't want to write all these JSONObjects yourself all the time you can use the Graph SDK for c# (also eliminates the risk that you forget a property/add a wrong one). They already have an object for Appointments called Event.

    Edit

    You also don't need the "myDomain" part in your request URL.

    0 讨论(0)
提交回复
热议问题