From c2ab0ad641b60b453a0697918e9db7773e89a6f6 Mon Sep 17 00:00:00 2001 From: Claus Vium Date: Fri, 21 Jun 2019 19:08:04 +0200 Subject: [PATCH] Wait for the async authentication to finish when the JTW token expires --- .../TV/TheTVDB/TvDbClientManager.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs b/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs index 1d1fbd00f1..85833223e1 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs @@ -24,24 +24,28 @@ namespace MediaBrowser.Providers.TV.TheTVDB { _cache = memoryCache; _tvDbClient = new TvDbClient(); - _tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey); - _tokenCreatedAt = DateTime.Now; } - public TvDbClient TvDbClient + private TvDbClient TvDbClient { get { + if (string.IsNullOrEmpty(_tvDbClient.Authentication.Token)) + { + _tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey).GetAwaiter().GetResult(); + _tokenCreatedAt = DateTime.Now; + } + // Refresh if necessary if (_tokenCreatedAt < DateTime.Now.Subtract(TimeSpan.FromHours(20))) { try { - _tvDbClient.Authentication.RefreshTokenAsync(); + _tvDbClient.Authentication.RefreshTokenAsync().GetAwaiter().GetResult(); } catch { - _tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey); + _tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey).GetAwaiter().GetResult(); } _tokenCreatedAt = DateTime.Now;