【问题标题】:"The scope https://graph.microsoft.com/Calendars.Read is not valid" error while getting access token for my app获取我的应用程序的访问令牌时出现“范围 https://graph.microsoft.com/Calendars.Read 无效”错误
【发布时间】:2019-11-08 08:22:53
【问题描述】:

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

我收到以下错误:

错误:范围 https://graph.microsoft.com/Calendars.Read 不是 有效。

下面是我的代码:

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
            }

我在这里做错了什么?我已经授予 Calendars 的权限。在那里注册我的应用时在 azure 门户中阅读:https://www.screencast.com/t/jTjnB4SX5I

【问题讨论】:

  • 有趣。尝试将范围设为 Calendars.Read
  • 我也试过了,但结果一样。开始给The scope Calendars.Read is not valid.
  • 啊,错过了您正在执行客户端凭据流。在这种情况下,请尝试仅使用范围 https://graph.microsoft.com/.default
  • 我实际上已经尝试过并获得了访问令牌:) 但是当我使用该令牌为用户读取日历事件时,我收到此错误:"Code: NoPermissionsInAccessToken, Message: The token contains no permissions, or permissions can not be understood."
  • 我试图获取事件的代码: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();

标签: c# azure microsoft-graph-api


【解决方案1】:

这里发生了一些事情。

  1. 当您使用客户端凭据流时,您需要使用{resource}/.default 形式的范围,其中{resource} 是您要访问的事物的URL。在这种情况下,您的范围应该是https://graph.microsoft.com/.default。 (Source)
  2. 您没有为您的应用注册配置任何应用权限。从您的屏幕截图中,您只配置了委派权限,即需要登录用户的用户权限。将Calendars.Read 添加为应用程序权限,这样您就可以开始使用了。

【讨论】:

  • 谢谢 Jason,如何将其添加为应用程序权限?我在 azure 门户中没有看到选项。可能在这里遗漏了一些东西。但是在 Enterprise Applications > Permission 我看到这个:screencast.com/t/STU1c8jXThanks。
  • 能够弄清楚这一点。干杯。
猜你喜欢
  • 2016-11-08
  • 1970-01-01
  • 2020-10-05
  • 2018-02-06
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多