Post 以编程方式将事件发送到 Outlook 日历 c# ASP.NET

Post event to Outlook Calendar programmatically c# ASP.NET

我正在将 Outlook 日历与具有 客户端凭据流 的自定义日历集成,并且我正在尝试制作我自己的 api 并且不太使用 MAPI .

我想Post到https://outlook.office.com/api/v2.0/TenantDomain/users/useremail@domain/events

我正在遵循本指南 Create Events 并制作了助手 类:

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

我的 json object 看起来像这样:

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

        };

我想做的是制作一个 Post 异步方法,如下所示:

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

但是这给了我 401 未经授权,我错过了什么吗?我需要在 Httpclient 中包含 accessToken 吗?

更新

我已经在请求 headers 中添加了令牌,但仍然得到 401 未经授权:

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn);

更新 2 在 header:

中包含 accessToken 后,我现在收到此错误

原因="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"

现在我迷路了,accesstoken需要在header还是在jsonobject的body?

更新 3 显然我需要更新我是如何获得 accessToken 的,我会 post 如果我这次能做对的话

感谢任何帮助!!

此问题已在另一个 post 中得到解答,请关注此 以获得答案。我在这里使用了错误的 requestURL,everytnig 在另一个 post :)

中有解释