c# - HttpProvider.SendAsync() 空内容

标签 c# microsoft-graph-api outlook-restapi

我正在尝试用 C# 构建一个小型应用程序,以从 Microsoft Graph API 检索建议的 session 时间。身份验证后,我调用graphClient.HttpProvider.SendAsync(t);希望获得建议的 session 时间。但是,通过断点单步执行,一切似乎都很顺利,直到该调用,然后 FindMeetingTimes 请求内容为空/空。

调用:eventsService.RunAsync();

internal async Task RunAsync()
    {
        try
        {

            // Create request object
            var findMeetingTimeRequest = new FindMeetingTimeRequestModel
            {
                Attendees = new List<AttendeeBase>
                {
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress {Address = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c11051d18180e190f0f3c1813111d1512521f1311" rel="noreferrer noopener nofollow">[email protected]</a>" },
                        Type = AttendeeType.Required
                    }
                },
                LocationConstraint = new LocationConstraint
                {
                    IsRequired = true,
                    SuggestLocation = false,
                    Locations = new List<LocationItemModel>
                {
                    new LocationItemModel{ DisplayName = "A116", Address = null, Coordinates = null }
                }
                },
                TimeConstraint = new TimeConstraintModel
                {
                    TimeSlots = new List<TimeSlotModel>
                {
                    new TimeSlotModel
                    {
                        Start = new DateTimeValueModel
                        {
                            Date = "2018-03-23",
                            Time = "08:00:00",
                            TimeZone = "Central Standard Time"
                        },
                        End = new DateTimeValueModel
                        {
                            Date = "2018-03-23",
                            Time = "09:00:00",
                            TimeZone = "Central Standard Time"
                        }
                    }
                }
                },
                MeetingDuration = new Duration("PT1H"),
                MaxCandidates = 99,
                IsOrganizerOptional = false,
                ReturnSuggestionHints = false
            };

            GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

            var t = graphClient.Me.FindMeetingTimes(findMeetingTimeRequest.Attendees, findMeetingTimeRequest.LocationConstraint, findMeetingTimeRequest.TimeConstraint, findMeetingTimeRequest.MeetingDuration, findMeetingTimeRequest.MaxCandidates, findMeetingTimeRequest.IsOrganizerOptional).Request().GetHttpRequestMessage();

            await graphClient.AuthenticationProvider.AuthenticateRequestAsync(t);

            var response = await graphClient.HttpProvider.SendAsync(t);
            var jsonString = await response.Content.ReadAsStringAsync();

            Console.WriteLine(jsonString);
            return;
        }catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            return;
        }
    }

我有点不知道下一步该尝试什么。我查看了示例,到目前为止只有少数使用 GraphServiceClient/SDKHelper 进行身份验证。这可能是问题的一部分吗?

我在 await graphClient.HttpProvider.SendAsync(t); 期间遇到了两个异常:

Exception thrown: 'Microsoft.Graph.ServiceException' in Microsoft.Graph.Core.dll

Exception thrown: 'System.NullReferenceException' in System.Web.dll


更新:使用下面 Michael 的评论中的引用和 FindMeetingTimes() 的空参数列表的原始代码,我收到了凭据异常: "Code: ErrorAccessDenied\r\nMessage: Access is denied. Check credentials and try again.\r\n\r\nInner error\r\n"

调用 await eventsService.EventFindMeetingsTimes(graphClient);

public async System.Threading.Tasks.Task EventFindMeetingsTimes(GraphServiceClient graphClient)
    {
        try
        {
            User me = await graphClient.Me.Request().GetAsync();

            // Get the first three users in the org as attendees unless user is the organizer.
            var orgUsers = await graphClient.Users.Request().GetAsync();
            List<Attendee> attendees = new List<Attendee>();
            Attendee attendee = new Attendee();
            attendee.EmailAddress = new EmailAddress();
            attendee.EmailAddress.Address = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f515e525a7f5b50525e5651115c5052" rel="noreferrer noopener nofollow">[email protected]</a>";
            attendees.Add(attendee);

            // Create a duration with an ISO8601 duration.
            Duration durationFromISO8601 = new Duration("PT1H");
            MeetingTimeSuggestionsResult resultsFromISO8601 = await graphClient.Me.FindMeetingTimes(attendees,
                                                                                                        null,
                                                                                                        null,
                                                                                                        durationFromISO8601,
                                                                                                        2,
                                                                                                        true,
                                                                                                        false,
                                                                                                        10.0).Request().PostAsync();
            List<MeetingTimeSuggestion> suggestionsFromISO8601 = new List<MeetingTimeSuggestion>(resultsFromISO8601.MeetingTimeSuggestions);
        }
        catch (Exception e)
        {
            Console.WriteLine("Something happened, check out a trace. Error code: {0}", e.Message);
        }
    }

当我使用 GraphExplorer 进行测试时,我用来登录的帐户可以正常工作。凭证/ token 是否有可能没有通过 Web 表单传递到图形客户端?


解决方案:Graph Docs example <-- 由 Michael 提供帮助正确设置格式。 Find meeting times problem #559 <-- Marc 透露,权限最终需要更新,并最终解决了我的更新问题。

最佳答案

您忘记在 SendAsync(t) 之前设置 HttpMethod。它使用 GET 而不是 POST。

t.Method = System.Net.Http.HttpMethod.Post;

话虽如此,我同意马克的观点。使用客户端库的内置功能:

https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/tests/Microsoft.Graph.Test/Requests/Functional/EventTests.cs#L88

关于c# - HttpProvider.SendAsync() 空内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49370597/

相关文章:

microsoft-graph-api - Microsoft Graph-用户API增量 token

microsoft-graph-api - 如何通过MS Graph API获取 session 室资源?

c# - 批量插入的问题

c# - Visual Studio 2008 与 2005 或 C# 3.0 与 C# 2.0 的新增功能是什么?

azure-active-directory - React/PyFlask - 正确的 Microsoft AD 登录架构

azure - 使用 AAD Graph API 或 Microsoft Graph API 获取 Azure Active Directory 应用程序权限

node.js - Outlook API 日历中的时差

microsoft-graph-api - 如何在特定时间间隔内从收件箱阅读我的消息或从特定电子邮件地址发送的邮件?

c# - Windows 控件可以与 WPF 一起使用吗?

c# - 图钉调整绑定(bind)缩放级别大小