MS 图形 API 呼叫掉线"The token contains no permissions, or permissions can not be understood."



使用以下代码会删除一个异常,并显示以下消息:"NoPermissionsInAccessToken令牌不包含权限,或者无法理解权限">

var clientId = "<GUID-goes-here>";  
var tenantId = "<GUID-comes-here>";  
var clientSecret = "<secret-goes-here>";
var scopes = new[] {"https://graph.microsoft.com/.default"};
var confidentialClientApplication = ConfidentialClientApplicationBuilder  
.Create(clientId) 
.WithClientSecret(clientSecret)          
.WithTenantId(tenantId)
.Build();  
var authResult = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().Result;
string token = authResult.AccessToken;
var authenticationProvider = new DelegateAuthenticationProvider(async (request) => {
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
await Task.FromResult<object>(null);
});
var graphClient = new GraphServiceClient(authenticationProvider);
var queryOptions1 = new List<QueryOption>()
{
new QueryOption("startDateTime", "2020-05-16" ),
new QueryOption("endDateTime", "2020-05-21"),
};
var calendar = graphClient.Me.Calendar.CalendarView
.Request(queryOptions1)
.OrderBy("start/dateTime")
.Top(3)
.GetAsync()
.Result;
// DROPS
// ServiceException: Code: NoPermissionsInAccessToken
// Message: The token contains no permissions, or permissions can not be understood.

应用程序已注册,已授予Calendars.Read、Calendars.Read.Shared权限。令牌值似乎不错——大约1400个字符长。

有人知道这里发生了什么吗?欢迎任何建议!谢谢

这里使用的是客户端凭据流,您需要为此指定应用程序权限,以便它们可以显示在令牌中。

您只需将令牌放入https://jwt.ms并查看您是否拥有令牌中的权限。

这里还有一件事你错过了,那就是你在代码中使用me,这意味着应该有一个用户如何登录这里,但没有用户,因为你使用的是应用程序上下文流。因此,只需添加应用程序权限,然后调用API,如下所示。

var calendarView = await graphClient.Users["userid"].CalendarView
.Request( queryOptions )
.GetAsync();

相关内容