该错误通常是因为应用未被授权访问当前用户的信息而导致的。添加适当的权限后,问题应该得到解决。以下是使用Graph API获取当前用户信息的示例代码:
var authResult = await cca.AcquireTokenByAuthorizationCode(scopes, authorizationCode)
.ExecuteAsync();
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) => {
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
}));
var user = await graphClient.Me.Request().GetAsync();
如果您使用的是Rest Client,可以尝试以下代码片段:
var httpClient = new HttpClient { BaseAddress = new Uri("https://graph.microsoft.com/") };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "[YOUR_TOKEN]");
var response = await httpClient.GetAsync("v1.0/me");
var content = await response.Content.ReadAsStringAsync();