【发布时间】: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