public async Task <Token> UpdateTokenAsync() { using (Metrics.CreateHistogram("stack_air_update_token_async_duration_seconds", "").NewTimer()) { await SemaphoreSlim.WaitAsync(); try { var infoItem = new InfoItem(_apiSecret.ClientId, _apiSecret.ClientSecret, _apiSecret.Scope, _apiSecret.AuthorityUrl); var url = _apiSecret.GraphQLUrl; var token = await _service.GetTokenAsync(infoItem); if (token == null) { _logger.LogError(2432, "Error occurred while fetch token."); return(null); } return(token); } finally { SemaphoreSlim.Release(); } } }
public async Task <bool> TryRequestTokenAsync() { try { if (_token != null && _expires > DateTimeOffset.Now) { return(true); } var infoItem = new InfoItem( _secret.ClientId, _secret.ClientSecret, _secret.Scope, _secret.AuthorityUrl); _token = await _identity.GetTokenAsync(infoItem); if (_token == null) { _logger.LogWarning(2432, $"Error occurred while request token Client: '{infoItem.ClientId}', Scope '{infoItem.Scope}'."); return(false); } _expires = DateTimeOffset.Now.AddSeconds(_token.ExpiresIn - 60); return(true); } catch (Exception ex) { _logger.LogError(2432, ex, $"Error occurred while request token."); return(false); } }