Exemplo n.º 1
0
        public virtual async Task <IToken> GetToken()
        {
            if (this.tokenReturned)
            {
                return(this.token);
            }

            if (this.AuthHttpServer == null)
            {
                return(null);
            }

            this.AuthHttpServer.AuthorizationFinished += this.AuthHttpServer_AuthorizationFinished;
            await this.AuthHttpServer.Start().ConfigureAwait(false);

            this.Authorize();
            bool shouldAbortAuthorization = false;

            while (this.authResponse == null && !shouldAbortAuthorization)
            {
                await Task.Delay(100).ConfigureAwait(false);

                shouldAbortAuthorization = await this.ShouldAbortAuthorization().ConfigureAwait(false);
            }

            if (this.authResponse == null && shouldAbortAuthorization)
            {
                this.AuthHttpServer.AuthorizationFinished -= this.AuthHttpServer_AuthorizationFinished;
                await this.AuthHttpServer.Stop().ConfigureAwait(false);

                return(null);
            }

            if (this.authResponse != null && this.authResponse.Error == null && !string.IsNullOrWhiteSpace(this.authResponse.Code))
            {
                if (this.authResponse.State == this.State || string.IsNullOrWhiteSpace(this.authResponse.State) && string.IsNullOrWhiteSpace(this.State))
                {
                    HttpResponse         httpResponse         = new HttpResponse(256, 1024);
                    SpotifyTokenResponse spotifyTokenResponse = new SpotifyTokenResponse(256, 32, 736);
                    AuthorizationCodeFlow.GetAuthorizationToken(ref httpResponse, ref spotifyTokenResponse, this.authResponse.Code);

                    if (httpResponse.status == (int)HttpStatusCode.OK)
                    {
                        spotifyTokenResponse.CreationDate = DateTime.Now;
                        this.token         = this.CreateToken(spotifyTokenResponse);
                        this.tokenReturned = true;
                    }
                    else
                    {
                        // TODO: Handle HTTP status != OK
                        logger.Debug("TODO: Handle HTTP status != OK");
                    }
                }
            }

            this.tokenReturned = true;
            return(this.token);
        }
Exemplo n.º 2
0
        public virtual async Task <IToken> GetToken()
        {
            if (this.tokenReturned)
            {
                return(this.token);
            }

            if (this.AuthHttpServer == null)
            {
                return(null);
            }

            this.AuthHttpServer.AuthorizationFinished += this.AuthHttpServer_AuthorizationFinished;
            await this.AuthHttpServer.Start();

            AuthorizationCodeFlow.Authorize(this.Scopes, this.State, this.ShowDialog);
            while (this.authResponse == null)
            {
                await Task.Delay(100);
            }

            if (this.authResponse.Error == null && !string.IsNullOrWhiteSpace(this.authResponse.Code))
            {
                if (this.authResponse.State == this.State)
                {
                    HttpResponse         httpResponse         = new HttpResponse(256, 1024);
                    SpotifyTokenResponse spotifyTokenResponse = new SpotifyTokenResponse(256, 32, 736);
                    AuthorizationCodeFlow.GetAuthorizationToken(ref httpResponse, ref spotifyTokenResponse, this.authResponse.Code);

                    if (httpResponse.status == (int)HttpStatusCode.OK)
                    {
                        spotifyTokenResponse.CreationDate = DateTime.Now;
                        this.token         = this.CreateToken(spotifyTokenResponse);
                        this.tokenReturned = true;
                        return(this.token);
                    }
                }
            }

            this.tokenReturned = true;
            return(null);
        }