title: Post events to Microsoft Graph c#
body: <p>sorry for the multiple posts, I can't get a post request to Microsoft Graph to work, I'm following this <a href="https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_post_events" rel="nofollow noreferrer">documentation</a>, I'm trying to create an event in </p>
  
  <p><a href="https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events" rel="nofollow noreferrer">https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events</a></p>
  
  <p>I have a function and some helper classes to create the json object like so:</p>
  
  <pre><code>List&lt;ToOutlookCalendar&gt; toOutlook = new List&lt;ToOutlookCalendar&gt;();    
  toOutlook.Add(new ToOutlookCalendar
          {
              Start = new End
              {
                  DateTime = DateTimeOffset.UtcNow,
                  TimeZone = "Pacific Standard Time"
              },
              End = new End
              {
                  DateTime = DateTimeOffset.UtcNow,
                  TimeZone = "Pacific Standard Time"
              },
              Body = new Body
              {
                  ContentType = "HTML",
                  Content = "testar for att se skit"
              },
              Subject = "testin",
              Attendees = new List&lt;Attendee&gt;
              {
                  new Attendee
                  {
                      EmailAddress = new EmailAddress
                      {
                          Address = "myEmail",
                          Name = "name"
                      },
                      Type = "Required"
  
                  }
  
              },
              Token = tokenn
  
          });
  
          return new JsonResult
          {
              Data = toOutlook
  
          };
  </code></pre>
  
  <p>previously I was posting to: <a href="https://outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events" rel="nofollow noreferrer">https://outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events</a></p>
  
  <p>which gave me an error 401, complaining about the token being to week. I created an x509 certificate but had no luck finding a way to upload it to my directory in azure and since I want to do everything programmatically and have succeeded so far and decided to take another approach and came upon the Microsoft graph documentation again.</p>
  
  <p>I get my calendar events from after having authorized the application permissions for Calendars.ReadWrite: <a href="https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events" rel="nofollow noreferrer">https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events</a>.</p>
  
  <p>Anyhow my request looks like this and gives me a 400 Bad Request:</p>
  
  <pre><code>htttpclient.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn);
  htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com");
  htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
  var stringPayload = await Task.Run(() =&gt; JsonConvert.SerializeObject(res));
  
  var response = await htttpclient.PostAsync($"https://graph.microsoft.com/beta/{myDomain}/users/{myEmail}/calendar/events",
  new StringContent(stringPayload, Encoding.UTF8, "application/json"));
  </code></pre>
  
  <p>Does anyone have any idea why? I'm following the documentation to the letter i believe but still get a 400 bad request. </p>
  
  <p><strong>Edit 1</strong> </p>
  
  <p>I use these classes to create the event based on the documentation</p>
  
  <pre><code> public class ToOutlookCalendar
      {
          [JsonProperty("Subject")]
          public string Subject { get; set; }
  
          [JsonProperty("Body")]
          public Body Body { get; set; }
  
          [JsonProperty("Start")]
          public End Start { get; set; }
  
          [JsonProperty("End")]
          public End End { get; set; }
  
          [JsonProperty("Attendees")]
          public List&lt;Attendee&gt; Attendees { get; set; }
      }
  
      public class Attendee
      {
          [JsonProperty("EmailAddress")]
          public EmailAddress EmailAddress { get; set; }
  
          [JsonProperty("Type")]
          public string Type { get; set; }
      }
  
      public class EmailAddress
      {
          [JsonProperty("Address")]
          public string Address { get; set; }
  
          [JsonProperty("Name")]
          public string Name { get; set; }
      }
  
      public class Body
      {
          [JsonProperty("ContentType")]
          public string ContentType { get; set; }
  
          [JsonProperty("Content")]
          public string Content { get; set; }
      }
  
      public class End
      {
          [JsonProperty("DateTime")]
          public DateTimeOffset DateTime { get; set; }
  
          [JsonProperty("TimeZone")]
          public string TimeZone { get; set; }
      }
  </code></pre>
  
  <p>Any help is appreciated!!</p>