protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            _ = request ?? throw new ArgumentNullException(nameof(request));

            var httpRequestHeaders = request.Headers;

            // If you have the following attribute in your interface, the authorization header will be "Bearer", not null.
            // [Headers("Authorization: Bearer")]
            // If we have a token, then we want to use that token - otherwise generate a service to service one.
            var authenticationHeaderValue = httpRequestHeaders.Authorization;

            if (authenticationHeaderValue != null)
            {
                if (authenticationHeaderValue.Scheme == "Bearer" && !string.IsNullOrWhiteSpace(authenticationHeaderValue.Parameter))
                {
                    var scopes = new List <string>()
                    {
                        "https://graph.microsoft.com/User.Read"
                    };
                    var accessToken = await tokenCreator.GetAccessTokenOnBehalfOf(scopes, authenticationHeaderValue.Parameter);

                    httpRequestHeaders.Authorization = new AuthenticationHeaderValue(authenticationHeaderValue.Scheme, accessToken);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(tokenCreatorConfiguration.TestUsername) && !string.IsNullOrWhiteSpace(tokenCreatorConfiguration.TestPassword))
                    {
                        var accessToken = await tokenCreator.GetIntegrationTestTokenAsync();

                        httpRequestHeaders.Authorization = new AuthenticationHeaderValue(authenticationHeaderValue.Scheme, accessToken);
                    }
                    else
                    {
                        var accessToken = await tokenCreator.GetClientApplicationAccessTokenAsync();

                        httpRequestHeaders.Authorization = new AuthenticationHeaderValue(authenticationHeaderValue.Scheme, accessToken);
                    }
                }
            }

            return(await base.SendAsync(request, cancellationToken));
        }
        private GraphServiceClient BuildGraphServiceClient(AuthenticationHeaderValue authenticationHeaderValue)
        {
            _ = authenticationHeaderValue ?? throw new ArgumentNullException(nameof(authenticationHeaderValue));

            var delegateAuthenticationProvider = new DelegateAuthenticationProvider(
                async(requestMessage) =>
            {
                var scopes = new List <string>()
                {
                    "https://graph.microsoft.com/User.Read", "https://graph.microsoft.com/OnlineMeetings.ReadWrite"
                };
                var userToken = await tokenCreator.GetAccessTokenOnBehalfOf(scopes, authenticationHeaderValue.Parameter);

                // Append the access token to the request.
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", userToken);
            });

            var graphServiceClient = new GraphServiceClient(delegateAuthenticationProvider);

            return(graphServiceClient);
        }