访问令牌验证失败Microsoft Graph API

约翰·斯普

我正在console application用C#建立一个
我想对Microsoft Graph API进行一些调用,以访问和编辑SharePoint中的一些Excel文件,以便可以自动执行组织中的某些流程。


该应用程序的逻辑很简单。

  1. 我打电话Azure Active Directory验证使用该控制台应用程序的客户端凭证流,这意味着我们将提供clientsID和的AppKey。我从Azure Active Directory>应用程序注册中获取了clientID和AppKey 在此处输入图片说明
  2. Then I want to receive the access token and use this to make a GET Request to the Microsoft Graph API.
    E.g https://graph.microsoft.com/v1.0/me/

    But then response I get is this:

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure. Invalid audience.",
    "innerError": {
      "request-id": "0a3ec**************",
      "date": "2019-10-15T13:54:33"
    }
  }
}

Below you will find the full code of my application with the two methods of getting the access token and calling the 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;
        }

        
    }
}

I have given all the access required for the app to run. Also I run the query on Graph Explorer and runs properly.
在此处输入图片说明 Why do I get this error on the console application?

Eastman

Ideally, the resource should actually be

private const string resource = "https://graph.microsoft.com";

But you still need to select the scopes that you want to target in your application. The way you are doing it at the moment does seem to acquire/set the relevant scopes which is done for you by Graph Explorer.

我建议按照此快速入门教程中有关如何构建点网核心控制台应用程序的操作,您应该立即启动并运行。它使用的MSAL库比您在方案中使用的ADAL库更好。

https://docs.microsoft.com/zh-cn/graph/tutorials/dotnet-core

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Microsoft Graph-用户API增量令牌

访问Microsoft Graph API的方法

获取Microsoft Graph和单个服务API终结点(Outlook REST API等)的访问令牌

为Microsoft Graph API获取有效的访问令牌

无法根据访问令牌对用户进行身份验证-MS Graph API C#

Microsoft Graph API不返回刷新令牌

如何从节点脚本获取Microsoft Graph API访问令牌?

使用PHP从Microsoft Graph API获取访问令牌

Microsoft Graph API:省略用户访问令牌

Microsoft Graph API令牌验证失败

获取Java中Microsoft Graph API的令牌

MS Graph API-访问令牌验证失败

Microsoft Graph API的访问令牌立即过期

microsoft-graph api:从图中的刷新令牌获取新的访问令牌,而无需重定向URL

Microsoft graph API:无法使用生成的访问令牌获取用户

Microsoft Graph API-如何在没有授权码的情况下获取访问令牌?

Microsft Graph API访问令牌

使用访问令牌通过Microsoft Graph API与OneDrive进行交互

如何验证使用Microsoft Graph API生成的oauth令牌

Microsoft Graph API身份验证错误:“访问令牌验证失败。无效的受众”

从 Microsoft Graph API 接收访问令牌但不接收刷新令牌

Microsoft Graph API - 无法刷新访问令牌

如何使用 ajax 调用获取 Microsoft Graph API 访问令牌

Microsoft Graph API 创建组失败

无法成功验证来自 Microsoft Graph API 的访问令牌

如何使用 Microsoft Graph API 为应用设置访问令牌生存期

无法针对 Microsoft Graph API 颁发的令牌验证 Azure AD B2C 的签名

如何在 Microsoft Graph API 的访问令牌中添加权限

Microsoft Graph API 使用企业应用程序的访问令牌发送电子邮件