将事件发布到Microsoft Graph C# [英] Post events to Microsoft Graph c#

查看:55
本文介绍了将事件发布到Microsoft Graph C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,我无法收到多个帖子,因此我无法向Microsoft Graph发送帖子,我正在关注 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

        };

以前,我发布到: https://outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events

给我一​​个错误401,抱怨令牌要到星期.我创建了x509证书,但没有运气找到将其蔚蓝上传到我的目录的方法,并且由于我想以编程方式完成所有工作,并且到目前为止已经成功了,因此决定采用另一种方法,并再次提出了Microsoft图形文档. >

在我授权Calendars.ReadWrite后,我从日历事件中获取日历事件:

有人知道为什么吗?我正在跟据我相信的那封信,但仍然收到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; }
    }

感谢您的帮助!

解决方案

要简短一些/总结一下,这就是我用来创建事件的必要条件.

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

您不需要这些标题:

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

并且令牌不是Event对象的一部分,因此您应该将其从ToOutlookCalendar中删除.

如果您不想一直自己编写所有这些JSONObject,则可以使用用于c#的Graph SDK (还消除了忘记属性/添加错误属性的风险).他们已经有一个名为Event的约会对象.

修改

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

sorry for the multiple posts, I can't get a post request to Microsoft Graph to work, I'm following this documentation, I'm trying to create an event in

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

I have a function and some helper classes to create the json object like so:

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

        };

previously I was posting to: https://outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events

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.

I get my calendar events from after having authorized the application permissions for Calendars.ReadWrite: https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events.

Anyhow my request looks like this and gives me a 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"));

Does anyone have any idea why? I'm following the documentation to the letter i believe but still get a 400 bad request.

Edit 1

I use these classes to create the event based on the documentation

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

Any help is appreciated!!

解决方案

To make this a little shorter/summarize this is what i use to create an event and is necessary.

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

You don't need these headers:

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

And Token is not Part of an Event object so you should remove it from ToOutlookCalendar.

If you don't want to write all these JSONObjects yourself all the time you can use the Graph SDK for c# (also eliminates the risk that you forget a property/add a wrong one). They already have an object for Appointments called Event.

Edit

You also don't need the "myDomain" part in your request URL.

这篇关于将事件发布到Microsoft Graph C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆