Microsoft.Graph.AttendeeType 枚举不正确

Microsoft.Graph.AttendeeType not enumerated correctly

我正在尝试通过 Microsoft Graph API 创建一个活动,邀请用户作为与会者。设置与会者的代码如下:

var attendees = new List<Microsoft.Graph.Attendee>();
        foreach (var e in emailAddresses)
        {
            var userProfile = await AzureGraph.GetOtherUserProfile(e);
            if (e != currentUserEmail.First())
            {
                Microsoft.Graph.EmailAddress email = new Microsoft.Graph.EmailAddress();
                email.Name = userProfile.DisplayName;
                email.Address = e;
                attendees.Add(new Microsoft.Graph.Attendee()
                {
                    EmailAddress = email,
                    Type = Microsoft.Graph.AttendeeType.Optional
                });
            }
        }

await AzureGraph.AddEvent(new Microsoft.Graph.Event
        {
            Subject = string.Format("Follow Up: {0}", Id),
            Body = new Microsoft.Graph.ItemBody
            {
                Content = "content"
            },
            Start = start,
            End = start.AddMinutes(30),
            Attendees = attendees
        });

但是,在发出请求时,我收到错误请求响应。原因是与会者的 'Type' 是 Microsoft.Graph.AttendeeType 的 n 枚举,并且未正确枚举。因此,它试图通过数值“1”而不是字符串值 "Optional" 发送,导致它失败。

我能够使用 fiddler 确认这一点,如果我手动将数值更改为字符串值,那么它就没有问题。

有没有人遇到过这个问题或有任何想法可以解决这个问题?

非常感谢您的提前帮助:)

我现在已经设法解决了这个问题。该解决方案有点麻烦,但它确实有效。我原来的调用代码如下:

public static async Task AddEvent(Event e)
{
  using (var client = new HttpClient())
  {
    using (var req = new HttpRequestMessage(HttpMethod.Post, _calendarUrl))
    {
        var token = await GetToken();
        req.Headers.Add("Authorization", string.Format("Bearer {0}", token));
        req.Headers.TryAddWithoutValidation("Content-Type", "application/json");

        var requestContent = JsonConvert.SerializeObject(new
        {
            Subject = e.Subject,
            Body = new
            {
                ContentType = "HTML",
                Content = e.Body.Content
            },
            Start = new
            {
                DateTime = e.Start,
                TimeZone = "UTC"
            },
            End = new
            {
                DateTime = e.End,
                TimeZone = "UTC"
            }
        });
        req.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");
        using (var response = await client.SendAsync(req))
        {
            if (response.IsSuccessStatusCode)
            {
                return;
            }
            else
            {
                throw new HttpRequestException("Event could not be added to calendar");
            }
        }
    }
}
}

我现在将其更改为:

    public static async Task AddEvent(Event e)
    {
        using (var client = new HttpClient())
        {
            using (var req = new HttpRequestMessage(HttpMethod.Post, _calendarUrl))
            {
                var token = await GetToken();
                req.Headers.Add("Authorization", string.Format("Bearer {0}", token));
                req.Headers.TryAddWithoutValidation("Content-Type", "application/json");

                IList<Attendee> attendees = new List<Attendee>();

                foreach(var a in e.Attendees)
                {
                    attendees.Add(new Attendee()
                    {
                        EmailAddress = a.EmailAddress,
                        Type = Enum.GetName(typeof(AttendeeType), AttendeeType.Optional)
                    });
                }

                var requestContent = JsonConvert.SerializeObject(new
                {
                    Subject = e.Subject,
                    Body = new
                    {
                        ContentType = "HTML",
                        Content = e.Body.Content
                    },
                    Start = new
                    {
                        DateTime = e.Start,
                        TimeZone = "UTC"
                    },
                    End = new
                    {
                        DateTime = e.End,
                        TimeZone = "UTC"
                    },
                    Attendees = attendees
                });
                req.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");
                using (var response = await client.SendAsync(req))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        return;
                    }
                    else
                    {
                        throw new HttpRequestException("Event could not be added to calendar");
                    }
                }
            }
        }
    }

同时添加以下本地class:

    private class Attendee
    {
        public EmailAddress EmailAddress { get; set; }
        public string Type { get; set; }
    }

本质上,Graph 与会者预期:
1. 包含 Name(字符串)和 Email(字符串)的 EmailAddress 对象。
2. AttendeeType 类型的类型对象,它是未正确传递的枚举。

因此,我创建了自己的 class Attendee 版本,以包含与 API 期望的相同的 EmailAddress 对象和字符串类型。

然后我不得不将枚举类型更改为枚举的名称而不是 int 值。这是按如下方式完成的:

attendees.Add(new Attendee()
{
    EmailAddress = a.EmailAddress,
    Type = Enum.GetName(typeof(AttendeeType), AttendeeType.Optional)
});

这给了我值 "Optional" 而不是 1,这使得 API.

可以接受

我希望这对以后的人有所帮助。

微软在代码中使用枚举并期望在 API 中使用字符串而不是整数,这似乎是微软的重大疏忽,我认为这需要解决。