private async Task StartEndlessAccessTokenUpdatingAsync(AccessTokenUpdatingArg tokenUpdatingArg, int firstDelaySec = 2) { int delaySec = firstDelaySec; while (true) { await Task.Delay(1000 *delaySec); RefreshedData result = TokenManagerService.UpdateAccessToken(tokenUpdatingArg).Result; lock (lockObj) { accessToken = result.AccessToken; } delaySec = result.ExpiresInSec; } // ReSharper disable once FunctionNeverReturns }
public async Task Initialize() { GoogleApiProfile googleApiProfile = profileStorageService.GetCurrentProfile(); try { GoogleApiAuthData authData; bool haveRefreshToken = googleApiProfile.GoogleApiData != null; if (haveRefreshToken) { Console.WriteLine("Refresh token уже есть"); authData = JsonConvert .DeserializeObject <GoogleApiAuthData>(googleApiProfile.GoogleApiData); } else { Console.WriteLine("Создание нового refresh токена"); InitializeAccessTokenArg initializeAccessTokenArg = googleApiProfile.GetGoogleApiInitArg(); authData = await TokenManagerService.CreateRefreshTokenAsync(initializeAccessTokenArg); string text = JsonConvert.SerializeObject(authData); Console.WriteLine(text); throw new Exception("Введите refresh token в текущий google api profile " + text); } if (authData != null) { AccessTokenUpdatingArg test = new AccessTokenUpdatingArg() { ClientId = googleApiProfile.ClientId, ClientSecret = googleApiProfile.ClientSecret, RefreshToken = authData.RefreshToken }; Task task = StartEndlessAccessTokenUpdatingAsync(test); } } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); } }