Post 事件到 Microsoft Graph c#

Post events to Microsoft Graph c#

很抱歉有多个 post,我无法向 Microsoft Graph 发送 post 请求,我正在关注此 documentation,我正在尝试在

中创建活动

https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events

我有一个函数和一些助手 类 来创建 json 对象,如下所示:

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 = "myEmail",
                        Name = "name"
                    },
                    Type = "Required"

                }

            },
            Token = tokenn

        });

        return new JsonResult
        {
            Data = toOutlook

        };

之前我post打算:https://outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events

这给了我一个错误 401,抱怨令牌过期了。我创建了一个 x509 证书,但没有找到一种方法将它上传到我的天蓝色目录,因为我想以编程方式完成所有事情并且到目前为止已经成功并决定采用另一种方法并再次出现 Microsoft 图形文档。

我在授权 Calendars.ReadWrite 的应用程序权限后获取我的日历事件:https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events

无论如何我的请求看起来像这样并给了我一个 400 Bad Request:

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(() => 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"));

有人知道为什么吗?我正在按照我相信的信中的文档进行操作,但仍然收到 400 错误请求。

编辑 1

我使用这些 类 根据文档创建事件

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

感谢任何帮助!!

让这个变得更简单 shorter/summarize 这是我用来创建活动的,并且是必要的。

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

你不需要这些headers:

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

并且令牌不是事件的一部分 object 因此您应该将其从 ToOutlookCalendar 中移除。

如果您不想一直自己编写所有这些 JSONObjects,您可以使用 Graph SDK for c#(也消除了您忘记 property/add 一个错误的风险)。他们已经有一个名为 Event 的 object 约会。

编辑

您也不需要请求中的 "myDomain" 部分 URL。