public override async Task GetAuthenticatedApiClient()

in src/YouTrackSharp/BearerTokenConnection.cs [76:134]


        public override async Task<YouTrackClient> GetAuthenticatedApiClient()
        {
            // Initialize HTTP client
            if (_youTrackClient == null)
            {
                var handler = new BearerTokenHttpClientHandler(_bearerToken);

                _configureHandler?.Invoke(handler);
                
                handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

                _httpClient = new HttpClient(handler)
                {
                    BaseAddress = ServerUri,
                    Timeout = _timeout
                };

                _youTrackClient = new YouTrackClient(_httpClient);
            }
            
            // Authenticate?
            if (_authenticated)
            {
                return _youTrackClient;
            }

            try
            {
                _authenticated = true;
                var response = await _youTrackClient.UsersMeAsync("id,guest");
                if (response.Guest == true || response.Guest == null)
                {
                    throw new UnauthorizedConnectionException(
                        Strings.Exception_CouldNotAuthenticate, (HttpStatusCode)200, "YouTrack responds that current user is guest");
                }
            }
            catch (YouTrackErrorException e)
            {
                throw new UnauthorizedConnectionException(
                    Strings.Exception_CouldNotAuthenticate, (HttpStatusCode)e.StatusCode, e.Response);
            }

            try
            {
                var me = await _youTrackClient.HubApiUserGetAsync("me", "guest");
                if (me.Guest == true || me.Guest == null)
                {
                    throw new UnauthorizedConnectionException(
                        Strings.Exception_CouldNotAuthenticate, HttpStatusCode.OK, "Hub responds that current user is guest");
                }
            }
            catch (YouTrackErrorException e)
            {
                throw new UnauthorizedConnectionException(
                    Strings.Exception_CouldNotAuthenticate, (HttpStatusCode)e.StatusCode, e.Response);
            }

            return _youTrackClient;
        }