【问题标题】:Post events to Microsoft Graph c#将事件发布到 Microsoft Graph c#
【发布时间】:2018-12-12 19:37:39
【问题描述】:

对于多个帖子感到抱歉,我无法向 Microsoft Graph 发送帖子请求,我正在关注此 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

        };

之前我发帖到:https://outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events

这给了我一个错误 401,抱怨令牌是周。我创建了一个 x509 证书,但没有找到将其上传到我的 azure 目录的方法,因为我想以编程方式完成所有操作并且到目前为止已经成功,因此决定采用另一种方法并再次找到 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; }
    }

感谢任何帮助!

【问题讨论】:

  • 您不需要请求正文中的令牌(仅在标头中)。尝试从“ToOutlookCalendar”类中删除“Token”属性。
  • 我完全忘记了 :) 但是我仍然收到错误 400 Bad Request
  • 您可以尝试删除htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com"); htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));。这些应该不是必需的(StringContent 中的 application/json 已经足够了)。
  • @MichaelHufnagel 你对 htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com"); 是对的,这不是必需的,只是我添加的东西,但根据文档:htttpclient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));是必要的
  • 是的,但是 PostAsync 中的 StringContent 应该已经将 Content-Type 标头设置为 application/json。 400 bad request 是否有任何错误消息传递?

标签: c# post office365 microsoft-graph-api outlook-restapi


【解决方案1】:

为了让它更短/总结一下,这是我用来创建事件的,并且是必要的。

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

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

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

编辑

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

【讨论】:

  • 抱歉,回复晚了,我昨天有点放弃了,开始使用谷歌日历:)我会测试一下,但我很确定我在实际请求之前做了一些愚蠢的事情,顺便说一句,我可以使用从 client_secret grant_type 获得的访问令牌吗?
  • 我的ToOutlookCalendar 的另一件事是一个功能,而不是一个类,我对你在这里要说的内容感到困惑“ToOutlookCalendar toOutlookCalendar = new ToOutlookCalendar();
  • 忽略最后一条评论 :)
  • 真的不知道你的意思是client_secret grant_type(可能是client credentials grant flow),但我想如果你的请求返回一个BadRequest错误,那么你使用的令牌应该没问题(如果不是你会得到另一个关于令牌的错误)。关于 OutlookCalendar 我认为您的代码中的 new ToOutlookCalendar 可能意味着它是一个对象^^。
  • 哦,当我检索我使用"grant_type", "client_credentials" 的访问令牌时,我很糟糕,我知道我可以将 client_assertion grant_type 与 x509 证书一起使用,但这对于使用我的应用程序的人来说会很麻烦,我得到了 401之前,但我向错误的端点发出请求:)。 new ToOutlookCalendar 现在是一个列表,好消息是有效负载不再为空,但现在它抱怨 write requests (excluding delete) must contain the content-type header declaration
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
  • 2018-06-24
  • 2019-03-23
  • 2023-04-06
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多