Microsoft Graph API 待办事项列表调用(无法将 ITodoListCollectionPage 转换为待办事项)
Microsoft Graph API To Do List Call (Cannot Convert ITodoListCollectionPage to Todo)
我正在创建一个 .Net Core 应用程序,用户可以在其中 login/logout 使用他们的 Microsoft 365 帐户,还可以通过 Microsoft Graph API 查看他们的任务 Lists/Tasks。我目前正在尝试通过 API 使用请求列表。
Controller.cs
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> Tasks()
{
User currentUser = null;
Todo currentLists = null;
try
{
currentUser = await _graphServiceClient.Me.Request().GetAsync();
currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
}
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))
{
try
{
Console.WriteLine($"{svcex}");
string claimChallenge = WwwAuthenticateParameters.GetClaimChallengeFromResponseHeaders(svcex.ResponseHeaders);
_consentHandler.ChallengeUser(_graphScopes, claimChallenge);
return new EmptyResult();
}
catch (Exception ex2)
{
_consentHandler.HandleException(ex2);
}
}
ViewData["Me"] = currentUser;
ViewData["Lists"] = currentLists;
return View();
}
但是这引发了一个问题
currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
无法转换为 Todo 类型以便我随后在以下 cshtml 页面上使用数据
@using Newtonsoft.Json.Linq
@{
ViewData["Title"] = "User Profile fetched from MS Graph";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />
<table class="table table-striped table-condensed" style="font-family: monospace" border="1">
<thead>
<tr>
</tr>
@{
var lists = ViewData["lists"] as Microsoft.Graph.TodoTaskList;
<tr>
<th>
ID
</th>
<th>
@lists.DisplayName
</th>
<th></th>
</tr>
}
</thead>
<tbody>
</tbody>
将 currentLists 转换为建议的类型“ITodoListCollectionsPage”会导致 Web 应用程序现在允许访问所请求的数据。
如何解决此问题并调用 Microsoft Graph API 以获取 Todo 任务列表并正确输出
await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
returns ITodoListCollectionsPage
.
您需要遍历所有页面并获取所有 ToDo 任务列表。
var currentLists = new List<TodoTaskList>();
var todoListsPage = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
currentLists.AddRange(todoListsPage.CurrentPage.ToList());
while (todoListsPage.NextPageRequest != null)
{
todoLists = await todoListsPage.NextPageRequest.GetAsync();
currentLists.AddRange(todoListsPage.CurrentPage.ToList());
}
如果您想获得待办事项任务,您需要为 List<TodoTaskList>
中的每个 TodoTaskList
提出另一个请求
仅示例如何获取待办事项:
var todoTasksDict = new Dictionary<string, List<TodoTask>>();
foreach (var todoList in currentLists)
{
var todoTasks = new List<TodoTask>();
var todoTasksPage = await client.Me.Todo.Lists[todoList.Id].Tasks.Request().GetAsync();
todoTasks.AddRange(todoTasksPage.CurrentPage.ToList());
while (todoTasksPage.NextPageRequest != null)
{
todoTasksPage = await todoTasksPage.NextPageRequest.GetAsync();
todoTasks.AddRange(todoTasksPage.CurrentPage.ToList());
}
todoTasksDict[todoList.DisplayName] = todoTasks;
}
我不知道你想如何显示待办任务列表和相关的待办任务,但你需要修改你的视图。
我正在创建一个 .Net Core 应用程序,用户可以在其中 login/logout 使用他们的 Microsoft 365 帐户,还可以通过 Microsoft Graph API 查看他们的任务 Lists/Tasks。我目前正在尝试通过 API 使用请求列表。
Controller.cs
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> Tasks()
{
User currentUser = null;
Todo currentLists = null;
try
{
currentUser = await _graphServiceClient.Me.Request().GetAsync();
currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
}
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))
{
try
{
Console.WriteLine($"{svcex}");
string claimChallenge = WwwAuthenticateParameters.GetClaimChallengeFromResponseHeaders(svcex.ResponseHeaders);
_consentHandler.ChallengeUser(_graphScopes, claimChallenge);
return new EmptyResult();
}
catch (Exception ex2)
{
_consentHandler.HandleException(ex2);
}
}
ViewData["Me"] = currentUser;
ViewData["Lists"] = currentLists;
return View();
}
但是这引发了一个问题
currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
无法转换为 Todo 类型以便我随后在以下 cshtml 页面上使用数据
@using Newtonsoft.Json.Linq
@{
ViewData["Title"] = "User Profile fetched from MS Graph";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />
<table class="table table-striped table-condensed" style="font-family: monospace" border="1">
<thead>
<tr>
</tr>
@{
var lists = ViewData["lists"] as Microsoft.Graph.TodoTaskList;
<tr>
<th>
ID
</th>
<th>
@lists.DisplayName
</th>
<th></th>
</tr>
}
</thead>
<tbody>
</tbody>
将 currentLists 转换为建议的类型“ITodoListCollectionsPage”会导致 Web 应用程序现在允许访问所请求的数据。
如何解决此问题并调用 Microsoft Graph API 以获取 Todo 任务列表并正确输出
await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
returns ITodoListCollectionsPage
.
您需要遍历所有页面并获取所有 ToDo 任务列表。
var currentLists = new List<TodoTaskList>();
var todoListsPage = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
currentLists.AddRange(todoListsPage.CurrentPage.ToList());
while (todoListsPage.NextPageRequest != null)
{
todoLists = await todoListsPage.NextPageRequest.GetAsync();
currentLists.AddRange(todoListsPage.CurrentPage.ToList());
}
如果您想获得待办事项任务,您需要为 List<TodoTaskList>
TodoTaskList
提出另一个请求
仅示例如何获取待办事项:
var todoTasksDict = new Dictionary<string, List<TodoTask>>();
foreach (var todoList in currentLists)
{
var todoTasks = new List<TodoTask>();
var todoTasksPage = await client.Me.Todo.Lists[todoList.Id].Tasks.Request().GetAsync();
todoTasks.AddRange(todoTasksPage.CurrentPage.ToList());
while (todoTasksPage.NextPageRequest != null)
{
todoTasksPage = await todoTasksPage.NextPageRequest.GetAsync();
todoTasks.AddRange(todoTasksPage.CurrentPage.ToList());
}
todoTasksDict[todoList.DisplayName] = todoTasks;
}
我不知道你想如何显示待办任务列表和相关的待办任务,但你需要修改你的视图。