1
votes

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!!

1
You do need to include an accessToken in the HttpClientkjr1995
I did, var htttpclient = new HttpClient(); htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); htttpclient.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn); but still gives 401Ako
How are you getting the token?kjr1995
I am the admin of my own company that i created so first I authorize the app to get calendar events, I have added write privlages aswell, After the authorization I make a Post to azure token endpoint and retrive the accesstoken. Getting events work fine but posting events is giving me a headache..Ako
Look at using Microsoft's graph api. It lets you do what you want but is really well documents.kjr1995

1 Answers

0
votes

This question has been answered in another post, follow this link for the answer. I was using the the wrong requestURL here, everytnig is explained in the other post :)