C# &引用;范围https://graph.microsoft.com/Calendars.Read 无效”;获取我的应用的访问令牌时出错

C# &引用;范围https://graph.microsoft.com/Calendars.Read 无效”;获取我的应用的访问令牌时出错,c#,azure,microsoft-graph-api,C#,Azure,Microsoft Graph Api,我正在尝试使用MS graph API访问用户的日历事件,但在尝试获取我在azure中注册的应用程序的访问令牌时 我得到以下错误: 错误:范围不正确 有效 下面是我的代码: string token = string.Empty; IConfidentialClientApplication app; app = ConfidentialClientApplicationBuilder.Create("ClientID")

我正在尝试使用MS graph API访问用户的日历事件,但在尝试获取我在azure中注册的应用程序的访问令牌时

我得到以下错误:

错误:范围不正确 有效

下面是我的代码:

string token = string.Empty;
            IConfidentialClientApplication app;
            app = ConfidentialClientApplicationBuilder.Create("ClientID")
                .WithTenantId("TenantID")
                .WithClientSecret("ClientSecret")
                .Build();

            string[] scopes = new string[] { "https://graph.microsoft.com/Calendars.Read" };

            AuthenticationResult result = null;

            try
            {
                result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
                token = result.AccessToken;

                var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
                    requestMessage
                        .Headers
                        .Authorization = new AuthenticationHeaderValue("bearer", token);

                    return Task.FromResult(0);
                }));

                var events = await graphServiceClient.Users["user1@onTestMicrosoft.com"].Events.Request().GetAsync();


            }
            catch (MsalServiceException ex)
            {
                // Case when ex.Message contains:
                // AADSTS70011 Invalid scope. The scope has to be of the form "https://resourceUrl/.default"
                // Mitigation: change the scope to be as expected
            }

我做错了什么?我已经授予日历权限。在azure portal中注册我的应用程序时请阅读:

这里发生了一些事情

  • 使用客户端凭据流时,需要使用
    {resource}/.default
    形式的作用域,其中
    {resource}
    是要访问的对象的URL。在这种情况下,您的范围应该是
    https://graph.microsoft.com/.default
    。()
  • 您尚未对应用程序注册配置任何应用程序权限。从屏幕截图中,您只配置了委派权限,即需要登录用户的用户权限。添加
    日历。将
    作为应用程序权限阅读,这应该会让您继续

  • 有趣。尝试将范围设置为日历。阅读。我也尝试过,但结果相同。已开始提供作用域日历。读取无效。啊,错过了您正在进行的客户端凭据流。在这种情况下,尝试只使用范围
    https://graph.microsoft.com/.default
    。我实际上已经尝试过了,并且获得了访问令牌:),但是当我使用该令牌读取用户的日历事件时,我得到了以下错误:
    “代码:NoPermissionsInAccessToken,消息:令牌不包含权限,或者权限无法理解。”
    My code我试图获取事件的位置:
    var-graphServiceClient=new-graphServiceClient(new-DelegateAuthenticationProvider((requestMessage)=>{requestMessage.Headers.Authorization=new-AuthenticationHeaderValue(“承载者”,令牌);返回任务。FromResult(0);});var events=await graphServiceClient.Users[”user1@onTestMicrosoft.com“].Events.Request().GetAsync()谢谢Jason,我如何将其添加为应用程序权限?我在azure门户中没有看到选项。这里可能遗漏了什么。然而,在企业应用程序>权限中,我看到了这样一个问题:谢谢。我们能够解决这个问题。干杯