我有一个使用MSAL和v2.0端点的应用程序来登录用户并获得令牌。
我最近将其更改为ADAL和正常的AAD端点(也更改了应用),现在当我尝试使用GraphService时出现以下错误:Current authenticated context is not valid for this request
以下是我使用的代码:
public static GraphServiceClient GetAuthenticatedClient()
{
GraphServiceClient graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string accessToken = await SampleAuthProvider.Instance.GetUserAccessTokenAsync();
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
return graphClient;
}
调用实际错误发生的方法:
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get events.
items = await eventsService.GetMyEvents(graphClient);
}
catch (ServiceException se)
{
}
获取令牌:
public async Task<string> GetTokenAsync()
{
ClientCredential cc = new ClientCredential(appId, appSecret);
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com");
AuthenticationResult result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", cc);
return result.AccessToken;
}
在网上找不到任何内容,所以我不知道如何继续。
答案 0 :(得分:2)
此异常是由使用客户端凭据流获取的令牌引起的。在此流程中,我没有上下文。
要解决此问题,您需要指定要获取的事件。或者您需要提供委托令牌。
代码供您参考:
//var envens=await graphClient.Me.Events.Request().GetAsync();
var envens = await graphClient.Users["xxx@xxx.onmicrosoft.com"].Events.Request().GetAsync();