gpt4 book ai didi

c# - 获取我的应用程序的访问 token 时出现 "The scope https://graph.microsoft.com/Calendars.Read is not valid"错误

转载 作者:行者123 更新时间:2023-12-03 02:49:42 25 4
gpt4 key购买 nike

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

我收到以下错误:

Error : The scope https://graph.microsoft.com/Calendars.Read is not valid.

下面是我的代码:

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["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0a0c1a0d4e3f10112b1a0c0b32161c0d100c10190b511c1012" rel="noreferrer noopener nofollow">[email protected]</a>"].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.Read 授予了权限,同时在那里注册我的应用程序: https://www.screencast.com/t/jTjnB4SX5I

最佳答案

这里发生了一些事情。

  1. 当您使用客户端凭据流程时,您需要使用 {resource}/.default 形式的范围,其中 {resource} 是网址您想要访问的内容。在这种情况下,您的范围应为 https://graph.microsoft.com/.default。 (Source)
  2. 您尚未在应用注册中配置任何应用程序权限。从您的屏幕截图来看,您仅配置了委派权限,这些权限是需要登录用户的用户权限。添加 Calendars.Read 作为应用程序权限,这应该可以帮助您继续。

关于c# - 获取我的应用程序的访问 token 时出现 "The scope https://graph.microsoft.com/Calendars.Read is not valid"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56771954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com