Exemple #1
0
        /// <summary>
        ///     Handles token refreshing when the access token expires
        /// </summary>
        /// <param name="hex">HttpRequestException exception</param>
        /// <param name="type">The service type</param>
        /// <returns>If we refreshed the refresh token</returns>
        private async Task <bool> HandleAuthTokenRefreshAsync(HttpRequestException hex, int type)
        {
            if (!hex.Message.ToLower().Contains("401") || !IsServiceConnected(type))
            {
                return(false);
            }

            try
            {
                // Get the token
                var userToken = Services.FirstOrDefault(x => x.Service == type)?.UserToken;
                if (userToken != null)
                {
                    var newToken = await AuthorizationHelpers.GetNewAuthTokenAsync(type, userToken.RefreshToken);

                    if (!string.IsNullOrEmpty(newToken.AccessToken))
                    {
                        userToken.AccessToken = newToken.AccessToken;
                    }

                    if (!string.IsNullOrEmpty(newToken.RefreshToken))
                    {
                        userToken.RefreshToken = newToken.RefreshToken;
                    }

                    if (!string.IsNullOrEmpty(newToken.ExpireTime))
                    {
                        userToken.ExpireTime = newToken.ExpireTime;
                    }

                    // Reconnect the service
                    ConnectService(type, userToken);

                    return(true);
                }
            }
            catch (SoundByteException)
            {
                DisconnectService(type, "There is an issue with your '" + type + "' account and SoundByte has disconnected it. To connect your account again, go to 'Accounts' and click login.");
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        ///     Handles token refreshing when the access token expires
        /// </summary>
        /// <param name="hex">HttpRequestException exception</param>
        /// <param name="type">The service type</param>
        /// <returns>If we refreshed the refresh token</returns>
        private async Task <bool> HandleAuthTokenRefreshAsync(HttpRequestException hex, ServiceType type)
        {
            if (!hex.Message.ToLower().Contains("401") || !IsServiceConnected(type))
            {
                return(false);
            }

            try
            {
                // Get the token
                var userToken = Services.FirstOrDefault(x => x.Service == type)?.UserToken;
                if (userToken != null)
                {
                    var newToken = await AuthorizationHelpers.GetNewAuthTokenAsync(type, userToken.RefreshToken);

                    userToken.AccessToken = newToken.AccessToken;
                    userToken.ExpireTime  = newToken.ExpireTime;

                    // Reconnect the service
                    ConnectService(type, userToken);
                }
            }
            catch (SoundByteException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SoundByteException("Error obtaining new access token.", ex.Message);
            }

            // Return after a slight
            await Task.Delay(500);

            return(true);
        }