Example #1
0
        /// <summary>
        /// Gets an authorized and ready to use SpotifyWebAPI by following the SecureAuthorizationCodeAuth process with its current settings.
        /// </summary>
        /// <returns></returns>
        public SpotifyWebAPI GetWebApi()
        {
            _lastAuth = new TokenSwapAuth(ExchangeServerUri, HostServerUri, Scope, HtmlResponse)
            {
                ShowDialog         = ShowDialog,
                MaxGetTokenRetries = MaxGetTokenRetries,
                TimeAccessExpiry   = true
            };

            _lastAuth.Start();

            _lastAuth.AuthReceived         += OnAuthReceived;
            _lastAuth.OnAccessTokenExpired += async(sender, e) =>
            {
                OnAccessTokenExpired?.Invoke(sender, AccessTokenExpiredEventArgs.Empty);

                if (AutoRefresh)
                {
                    await RefreshAuthAsync();
                }
            };

            OnExchangeReady?.Invoke(this, new ExchangeReadyEventArgs {
                ExchangeUri = _lastAuth.GetUri()
            });

            if (OpenBrowser)
            {
                _lastAuth.OpenBrowser();
            }

            if (!_authWait.WaitOne(Timeout * 1000))
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Authorization request has timed out."));
            }

            return(_lastWebApi);
        }
Example #2
0
        /// <summary>
        /// Gets an authorized and ready to use SpotifyWebAPI by following the SecureAuthorizationCodeAuth process with its current settings.
        /// </summary>
        /// <returns></returns>
        public async Task <SpotifyWebAPI> GetWebApiAsync()
        {
            return(await Task <SpotifyWebAPI> .Factory.StartNew(() =>
            {
                bool currentlyAuthorizing = true;

                // Cancel any ongoing get web API requests
                CancelGetWebApiRequest();

                lastAuth = new TokenSwapAuth(
                    exchangeServerUri: ExchangeServerUri,
                    serverUri: HostServerUri,
                    scope: Scope,
                    htmlResponse: HtmlResponse)
                {
                    ShowDialog = ShowDialog,
                    MaxGetTokenRetries = MaxGetTokenRetries,
                    TimeAccessExpiry = AutoRefresh || TimeAccessExpiry
                };
                lastAuth.AuthReceived += async(sender, response) =>
                {
                    if (!string.IsNullOrEmpty(response.Error) || string.IsNullOrEmpty(response.Code))
                    {
                        // We only want one auth failure to be fired, if the request timed out then don't bother.
                        if (!webApiTimeoutTimer.Enabled)
                        {
                            return;
                        }

                        OnAuthFailure?.Invoke(this, new AuthFailureEventArgs(response.Error));
                        currentlyAuthorizing = false;
                        return;
                    }

                    lastToken = await lastAuth.ExchangeCodeAsync(response.Code);

                    if (lastToken == null || lastToken.HasError() || string.IsNullOrEmpty(lastToken.AccessToken))
                    {
                        // We only want one auth failure to be fired, if the request timed out then don't bother.
                        if (!webApiTimeoutTimer.Enabled)
                        {
                            return;
                        }

                        OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Exchange token not returned by server."));
                        currentlyAuthorizing = false;
                        return;
                    }

                    if (lastWebApi != null)
                    {
                        lastWebApi.Dispose();
                    }
                    lastWebApi = new SpotifyWebAPI()
                    {
                        TokenType = lastToken.TokenType,
                        AccessToken = lastToken.AccessToken
                    };

                    lastAuth.Stop();

                    OnAuthSuccess?.Invoke(this, AuthSuccessEventArgs.Empty);
                    currentlyAuthorizing = false;
                };
                lastAuth.OnAccessTokenExpired += async(sender, e) =>
                {
                    if (TimeAccessExpiry)
                    {
                        OnAccessTokenExpired?.Invoke(sender, AccessTokenExpiredEventArgs.Empty);
                    }

                    if (AutoRefresh)
                    {
                        await RefreshAuthAsync();
                    }
                };
                lastAuth.Start();
                OnExchangeReady?.Invoke(this, new ExchangeReadyEventArgs {
                    ExchangeUri = lastAuth.GetUri()
                });
                if (OpenBrowser)
                {
                    lastAuth.OpenBrowser();
                }

                webApiTimeoutTimer = new System.Timers.Timer
                {
                    AutoReset = false,
                    Enabled = true,
                    Interval = Timeout * 1000
                };

                while (currentlyAuthorizing && webApiTimeoutTimer.Enabled)
                {
                    ;
                }

                // If a timeout occurred
                if (lastWebApi == null && currentlyAuthorizing)
                {
                    OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Authorization request has timed out."));
                }

                return lastWebApi;
            }));
        }