gpt4 book ai didi

c# - HttpProvider.SendAsync() 空内容

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:09 25 4
gpt4 key购买 nike

我正在尝试用 C# 构建一个小型应用程序,以从 Microsoft Graph API 检索建议的 session 时间。身份验证后,我调用 graphClient.HttpProvider.SendAsync(t); 希望获得建议的 session 时间。但是,使用断点逐步执行一切似乎都很好,直到该调用然后 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() 的空参数列表的原始代码,我得到了一个凭据异常: “代码:ErrorAccessDenied\r\n消息:访问被拒绝。检查凭据并重试。\r\n\r\n内部错误\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 进行测试时,我用于登录的帐户有效。凭据/ token 是否可能没有通过网络表单传递到图形客户端?


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

最佳答案

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

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

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com