错误
“权限不足,无法完成操作”
通常发生在Microsoft Entra ID应用程序没有执行操作所需的权限的情况下。
最初我得到了
相同的错误
:
为了解决该错误,
确保给予
User.Read.All
应用程序类型
API权限,因为您正在使用客户端凭据流。
-
使用者阅读全部的
使用用户交互流时,必须授予委托类型API权限。
授予
使用者阅读全部的
应用程序类型
API权限如下:
我能够成功地检索到具有UPN的用户,如下所示:
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models.ODataErrors;
class Program
{
static async Task Main(string[] args)
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "ClientID";
var tenantId = "TenantID";
var clientSecret = "ClientSecret";
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
try
{
var result = await graphClient.Users["[email protected]"].GetAsync();
Console.WriteLine($"User details: {result.DisplayName}, {result.Mail}, {result.Id}");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error?.Code);
Console.WriteLine(odataError.Error?.Message);
throw;
}
}
}
参考
Get a user - Microsoft Graph v1.0 | Microsoft