我正在尝试使用C#构建一个小型应用程序,以从Microsoft Graph API中检索建议的会议时间。验证之后,我致电graphClient.HttpProvider.SendAsync(t);希望获得建议的开会时间。但是,单步执行断点似乎一切正常,直到该调用,然后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 = "[email protected]" },
                        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 = "[email protected]";
            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进行测试时,我用于登录的帐户可以正常工作。凭证/令牌是否可能没有通过Web表单传递到图形客户端中?


解决方案:Graph Docs example Find meeting times problem #559

最佳答案

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

10-09 02:27