Microsoft.Graph.AttendeeType未正确枚举

时间:2016-02-18 10:16:18

标签: c# microsoft-graph azure-ad-graph-api

我正在尝试通过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
        });

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

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

有没有人遇到这个或有任何想法我怎么能解决这个问题?

非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

我现在设法解决了这个问题。解决方案有点像黑客,但它的工作原理。我原来的电话代码如下:

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

除了添加以下本地类:

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

基本上,图表参加者预期:
   1.包含Name(字符串)和Email(字符串)的EmailAddress对象。
   2.类型为AttendeeType的Type对象,它是未正确传递的枚举。

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

然后我必须将枚举类型更改为枚举的名称而不是int值。这样做如下:

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

这给了我“可选”的值而不是1,这使得API可以接受。

我希望将来可以帮助某人。

对于微软来说,在代码中使用枚举并期望在API中使用字符串而不是整数,这看起来像是一个主要的疏忽,我认为这需要解决。