'What happens to GraphServiceClient when access token expires?

This is the code. I get an access token and with that access token I create a client. What happens if access token expires? Do I need to create another client?. Create only one client? or each time I need users for example, I should call GetGraphServiceClient()? that will resolve the fact that the token is expired because calls for a new one. If not how can I verify if the token is expired?

public async Task<GraphServiceClient> GetGraphServiceClient()
{
    var token = await GetAccessToken();

    var client = new GraphServiceClient(new DelegateAuthenticationProvider(
        (requestMessage) =>
        {
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
            return Task.FromResult(0);
        }));

    return client;
}

    private async Task<string> GetAccessToken()
    {
        var app = ConfidentialClientApplicationBuilder.Create(_connectionData.ClientId)
       .WithAuthority(AzureCloudInstance.AzurePublic, _connectionData.TenantId)
       .WithClientSecret(_connectionData.ClientSecret)
       .Build();

        AuthenticationResult result = null;
        try
        {
            result = await app.AcquireTokenForClient(scopes)
            .ExecuteAsync();               
        }
        catch (MsalServiceException ex)
        {
            // Case when ex.Message contains: invalid scope
        }

        return result?.AccessToken;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source