I'm integrating Outlook Calendar with a custom calendar with client credential flow and I'm trying to make my own api and not use MAPI all to much.
I would like to make a Post to https://outlook.office.com/api/v2.0/TenantDomain/users/useremail@domain/events
I am following this guide Create Events and have made the helper classes:
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<Attendee> 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; }
}
my json object looks like this:
List<ToOutlookCalendar> toOutlook = new List<ToOutlookCalendar>();
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<Attendee>
{
new Attendee
{
EmailAddress = new EmailAddress
{
Address = "some email",
Name = "name"
}
},
new Attendee
{
EmailAddress = new EmailAddress
{
Address = "some email",
Name = "name"
}
}
}
});
return new JsonResult
{
Data = toOutlook,
ContentType = "application/json",
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
what i want to do is to make a PostAsync method like so:
var res = await ToOutlookKalendrar();
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(res));
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var responsse = await client.PostAsync($"https://outlook.office.com/api/v2.0/{tenantDomain}/users/{userEmail}/events", stringPayload );
However this gives me 401 unauthorized, Have I missed something? do I need to include the accessToken in the Httpclient?
Update
I have added the token in the request headers but still get 401 unauthorized:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn);
Update 2 I get this error now after having included the accessToken in the header:
reason="The access token is acquired using an authentication method that is too weak to allow access for this application. Presented auth strength was 1, required is 2.";error_category="invalid_token"
Now I'm lost, does the accesstoken need to be in the header or in the body of the json object?
Update 3 apperently I need to udate how i get the accessToken, I'll post the answer if I manage to do it right this time
Any help is appreciated!!