我正在用C#构建console application
。我想对 Microsoft Graph API 进行一些调用,以访问和编辑SharePoint中的一些Excel文件,以便使组织中的某些过程自动化。
该应用程序的逻辑很简单。
Azure Active Directory
来对控制台应用程序进行身份验证,这意味着我们将提供一个clientID和AppKey。我从Azure Active Directory>应用程序注册中获取了clientID和AppKey。例如
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"
}
}
}
在下面,您将找到我的应用程序的完整代码,其中包括获取访问 token 和调用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为您完成的相关范围。
我建议按照此快速入门教程中有关如何构建点网核心控制台应用程序的操作,您应该立即启动并运行。
它使用的MSAL库比您在方案中使用的ADAL库更好。
https://docs.microsoft.com/en-us/graph/tutorials/dotnet-core
关于c# - 访问 token 验证失败Microsoft Graph API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58396709/