我正在创建一个 .Net Core 应用程序,用户可以在其中使用他们的 microsoft 365 帐户登录/注销,还可以通过 Microsoft Graph API 查看他们的任务列表/任务。我目前正在尝试通过 API 使用来请求列表。
控制器.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 任务列表并正确输出