Microsoft Graph:当前经过身份验证的上下文对此请求无效

Microsoft Graph: Current authenticated context is not valid for this request

我有一个应用程序使用 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;
        }

在此网上找不到任何内容,所以我不确定如何继续。

错误:

此异常是由使用客户端凭据流获取的令牌引起的。在此流程中,Me.

没有上下文

要解决此问题,您需要指定要获取谁的事件。或者您需要提供 delegate-token.

供您参考的代码:

//var envens=await graphClient.Me.Events.Request().GetAsync();
var envens = await graphClient.Users["xxx@xxx.onmicrosoft.com"].Events.Request().GetAsync();