Azure AD Graph Education API中出现“禁止/访问被拒绝”错误

问题描述 投票:2回答:1

我正在使用Graph education API,想要有关用户配置文件的所有信息。在响应/ json对象中获得以下错误Forbidden AccessDenied未提供所需的声明值。

public async Task<ActionResult> GetUserDetails()
        {
            List<User> listUser = new List<User>();
            List<UserRole> userRole = new List<UserRole>();


            string clientId = configuration.GetValue<string>("AzureAd:ClientId");
            string clientSecret = configuration.GetValue<string>("AzureAd:ClientSecret");


            //var email = User.Identity.Name;

            //AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/LPExamDev.onmicrosoft.com/oauth2/token");
            AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/LPExamStaging.onmicrosoft.com/oauth2/token");
            ClientCredential creds = new ClientCredential(clientId, clientSecret);
            AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", creds);

            HttpClient http = new HttpClient();            
            string url = $"https://graph.microsoft.com/v1.0/education/users";  // Microsoft Education Graph

            //string url = $"https://graph.microsoft.com/v1.0/users"; // Microsoft Graph // Working fine.
            ////string url = "https://graph.windows.net/LPExamStaging.onmicrosoft.com/users?api-version=1.6"; 

            // Append the access token for the Graph API to the Authorization header of the request by using the Bearer scheme.
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            HttpResponseMessage response = await http.SendAsync(request);
            var json = await response.Content.ReadAsStringAsync();
            var jsonResponse = response.ToString();
            bool responseCode = response.IsSuccessStatusCode;
            //ViewBag.userData = json;

            //SaveAPIData(json);


            if (responseCode)
            {
                SaveAPIData(json);
            }
       }
azure-active-directory azure-ad-graph-api
1个回答
1
投票

您需要授予您的应用程序EduRoster.Read.All权限,然后单击“授予管理员同意”按钮。

enter image description here

登录azure portal->单击Azure Active Directory->单击应用程序注册(预览) - >单击您的应用程序 - >单击API权限 - >添加权限 - >选择应用程序权限

enter image description here

然后单击授予管理员同意按钮。

enter image description here

您可以使用https://jwt.io/解码您的访问令牌,以检查您是否已获得该权限。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.