我试图使用MS graph API访问用户的日历事件,但同时尝试获取我在天蓝色下注册的应用程序的访问令牌,
我收到以下错误:
错误:范围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
}
我在这里做错了什么?我已经授予了日历的权限。在其中登录我的应用程序时,请在azure门户中阅读:https://www.screencast.com/t/jTjnB4SX5I
答案 0 :(得分:1)
这里发生了几件事。
{resource}/.default
格式的范围,其中{resource}
是您要访问的内容的URL。在这种情况下,您的范围应为https://graph.microsoft.com/.default
。 (Source)Calendars.Read
作为应用程序许可,这将带您进入。