HttpProvider.SendAsync() 空内容

HttpProvider.SendAsync() Null Content

我正在尝试用 C# 构建一个小应用程序以从 Microsoft Graph API 检索建议的会议时间。身份验证后,我打电话给 graphClient.HttpProvider.SendAsync(t); 希望得到建议的会议时间。但是,在调用断点之前一切似乎都很好,然后 FindMeetingTimes 请求内容是 empty/null.

呼叫:eventsService.RunAsync();

internal async Task RunAsync()
    {
        try
        {

            // Create request object
            var findMeetingTimeRequest = new FindMeetingTimeRequestModel
            {
                Attendees = new List<AttendeeBase>
                {
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress {Address = "myaddress@domain.com" },
                        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 = "name@domain.com";
            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 进行测试时,我用于登录的帐户有效。有没有可能 credentials/token 没有通过 Web 表单传递到图形客户端?


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

没有更多细节,很难确定这里出了什么问题。也就是说,您应该从简化此代码开始。这至少会减少活动部件的数量:

var result = await graphClient.Me.FindMeetingTimes()
    .Request()
    .PostAsync();

if (!string.IsNullOrWhiteSpace(result.EmptySuggestionsReason))
{
    Console.WriteLine(result.EmptySuggestionsReason);
}
else
{
    foreach (var item in result.MeetingTimeSuggestions)
    {
        Console.WriteLine($"Suggestion: {item.SuggestionReason}");
    }
}

如果失败,请务必捕获整个异常并更新您的问题。

您忘记在 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