c# - 访问令牌验证失败 Microsoft Graph API
问题描述
我正在console application
用 C# 构建一个。
我想对Microsoft Graph API进行一些调用,以访问和编辑我的SharePoint中的一些 Excel 文件,以便我可以自动化我的组织中的一些流程。
应用程序的逻辑很简单。
- 我调用使用客户端凭证流
Azure Active Directory
来验证这个控制台应用程序,这意味着我们将提供一个 clientsID 和 AppKey。我从 Azure Active Directory > App Registrations获取了 clientsID 和 AppKey 。 - 然后我想接收访问令牌并使用它向Microsoft Graph API发出 GET 请求。
例如https://graph.microsoft.com/v1.0/me/
,但随后我得到的回应是:
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token validation failure. Invalid audience.",
"innerError": {
"request-id": "0a3ec**************",
"date": "2019-10-15T13:54:33"
}
}
}
您将在下面找到我的应用程序的完整代码,其中包含获取访问令牌和调用 Graph API 的两种方法:
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Tokens;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using AuthenticationContext = Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext;
namespace Project_Budget
{
class Program
{
private const string clientId = "14f1****************";
private const string aadInstance = "https://login.microsoftonline.com/{0}";
private const string tenant = "******.onmicrosoft.com";
private const string resource = "https://graph.windows.net";
private const string appKey = "IKV***********";
static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
private static HttpClient httpClient = new HttpClient();
private static AuthenticationContext context = null;
private static ClientCredential credential = null;
static void Main(string[] args)
{
context = new AuthenticationContext(authority);
credential = new ClientCredential(clientId,appKey);
Task<string> token = GetToken();
token.Wait();
//Console.WriteLine(token.Result + "\n");
Task<string> graphCall = GetExcelFile(token.Result);
graphCall.Wait();
Console.WriteLine(graphCall.Result + "\n");
Console.ReadLine();
}
private static async Task<string> GetExcelFile(string result)
{
string apiJsonResult = null;
var apiCallString = "https://graph.microsoft.com/v1.0/me/";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result);
var getResult = await httpClient.GetAsync(apiCallString);
if (getResult.Content != null)
{
apiJsonResult = await getResult.Content.ReadAsStringAsync();
}
return apiJsonResult;
}
private static async Task<string> GetToken()
{
AuthenticationResult result = null;
string token = null;
result = await context.AcquireTokenAsync(resource, credential); //authentication context object
token = result.AccessToken;
return token;
}
}
}
我已授予应用程序运行所需的所有访问权限。我也运行查询Graph Explorer
并正常运行。
为什么我在控制台应用程序上收到此错误?
解决方案
理想情况下,资源实际上应该是
private const string resource = "https://graph.microsoft.com";
但是您仍然需要选择要在应用程序中定位的范围。您目前执行此操作的方式似乎确实获取/设置了 Graph Explorer 为您完成的相关范围。
我建议您按照这个关于如何构建 dot net core 控制台应用程序的快速入门教程进行操作,您应该可以立即启动并运行。它使用 MSAL 库,该库比您在场景中使用的 ADAL 库更好。
https://docs.microsoft.com/en-us/graph/tutorials/dotnet-core
推荐阅读
- javascript - 如何在 Bootstrap 4 中更改工具提示箭头颜色?
- java - 如何解决 Spring Boot 应用程序中的 FlyWay 许可问题
- python - 我如何从网络上获得一定的价值
- javascript - Createjs 事件 - “mouseleave” 有效,但“click” 无效
- css - 尽管在 Opera 浏览器的 CSS 文件中添加了“resize:none”,但我在 HTML 中的 textarea 仍然可以调整大小
- r - 空间关系矩阵中的顺序点
- python - 无法将 PostgreSQL 数据库从 docker 连接到 python
- c# - 在没有原则的线程上使用 Authorize 属性时,Asp.net Core 操作不返回 401?
- swift - 无法使用 Fairplay HLS 从 AVPlayer 获取视频缩略图
- reporting-services - 在 SSRS 中,预览上的日期时间参数与部署的报告不匹配