public Task<SpotifyWebAPI> GetWebApi() { var authentication = new ImplicitGrantAuth { RedirectUri = $"{_redirectUrl}:{_listeningPort}", ClientId = _clientId, Scope = _scope, State = _xss }; AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false); SpotifyWebAPI spotifyWebApi = null; authentication.OnResponseReceivedEvent += (token, state) => { spotifyWebApi = HandleSpotifyResponse(state, token); authenticationWaitFlag.Set(); }; try { authentication.StartHttpServer(_listeningPort); authentication.DoAuth(); authenticationWaitFlag.WaitOne(_timeout); if (spotifyWebApi == null) throw new TimeoutException($"No valid response received for the last {_timeout.TotalSeconds} seconds"); } finally { authentication.StopHttpServer(); } return Task.FromResult(spotifyWebApi); }