HttpProvider.SendAsync()空内容

时间:2018-03-19 19:07:17

标签: c# microsoft-graph outlook-restapi

我正在尝试在C#中构建一个小应用程序,以从Microsoft Graph API检索建议的会议时间。在进行身份验证后,我会致电graphClient.HttpProvider.SendAsync(t);以获得建议的会议时间。但是,逐步执行断点,在调用之前一切正常,然后FindMeetingTimes请求内容为空/ 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进行测试时,我用来登录的帐户有效。是否有可能凭证/令牌不会通过网络表单传递到图表客户端?

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

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

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