如果您遇到此错误,请检查访问令牌的权限和范围是否正确。您还可以检查应用程序的访问策略和用户角色。 在以下示例中,我们将显示如何使用Graph API获取访问令牌并检查访问策略:
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string tenantId = "your_tenant_id";
string resource = "https://graph.microsoft.com";
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
AuthenticationResult authenticationResult = authContext.AcquireTokenAsync(resource, clientCredential).Result;
string accessToken = authenticationResult.AccessToken;
string policyUrl = "https://graph.microsoft.com/v1.0/policies/authenticationMethods/authenticationFactors";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage httpResponseMessage = httpClient.GetAsync(policyUrl).Result;
string resultContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
string userRoleUrl = "https://graph.microsoft.com/v1.0/me/memberOf";
httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
httpResponseMessage = httpClient.GetAsync(userRoleUrl).Result;
resultContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
您可以使用以上代码片段获取访问令牌并检查访问策略和用户角色。如果问题仍然存在,请检查您的租户是否有所更改,特别是访问策略和管理员角色。