예제 #1
0
        public async void Start()
        {
            try
            {
                _authenticator = new PKCEAuthenticator(_clientId !, TokenResponse);

                var config = SpotifyClientConfig.CreateDefault()
                             .WithAuthenticator(_authenticator);

                _spotifyClient = new SpotifyClient(config);

                var user = await _spotifyClient.UserProfile.Current();

                //var playlists = await _spotifyClient.Playlists.GetUsers(user.Id);


                _user = user;
                //UserPlaylists = playlists;

                if (user.Product == "premium")
                {
                    IsPremiumUser = true;
                }

                OnLoggedIn?.Invoke(_user, TokenResponse);
                _stateThread = new Thread(StateUpdateTimer);
                _stateThread.Start();
            }
            catch (Exception e)
            {
                //We will just ignore for now, this should be handled better though
            }
        }
예제 #2
0
    private void SetAuthenticator(PKCETokenResponse token)
    {
        // Set API authentification once recieved
        _pkceAuthenticator = new PKCEAuthenticator(_clientID, token);
        _pkceAuthenticator.TokenRefreshed += this.OnTokenRefreshed;

        OnAuthenticatorComplete?.Invoke(_pkceAuthenticator);
    }
예제 #3
0
        private async Task <IAuthenticator> AuthenticateFromCredentialsAsync()
        {
            var token = await ApiExt.LoadTokenAsync(_credentialsPath);

            var authenticator = new PKCEAuthenticator(_clientId, token);

            authenticator.TokenRefreshed += OnTokenRefreshed !;

            return(authenticator);
        }
예제 #4
0
파일: CreateClient.cs 프로젝트: garyng/Tss
        public async Task <ISpotifyClient> Handle(CreateClient request, CancellationToken cancellationToken)
        {
            var authenticator = new PKCEAuthenticator(_config.ClientId, request.Token);

            authenticator.TokenRefreshed += async(_, t) => await SaveToken(t);

            var config = SpotifyClientConfig.CreateDefault()
                         .WithAuthenticator(authenticator);

            await SaveToken(request.Token);

            return(new SpotifyClient(config));
        }
        private async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
        {
            await StopAndDisposeServer();

            var client        = new OAuthClient();
            var tokenResponse = await client.RequestToken(new PKCETokenRequest(SpotifyOptions.Value.ClientId, response.Code, new Uri(RedirectUri), Verifier));

            var authenticator = new PKCEAuthenticator(SpotifyOptions.Value.ClientId, tokenResponse);
            var config        = SpotifyClientConfig.CreateDefault().WithAuthenticator(authenticator);
            var spotifyClient = new SpotifyClient(config);

            RunSongExtractor(spotifyClient);
        }
예제 #6
0
        private void Start()
        {
            var json  = File.ReadAllText(CredentialsPath);
            var token = JsonConvert.DeserializeObject <PKCETokenResponse>(json);

            var authenticator = new PKCEAuthenticator(clientId, token);

            authenticator.TokenRefreshed += (sender, tokens) => File.WriteAllText(CredentialsPath, JsonConvert.SerializeObject(tokens));

            var config = SpotifyClientConfig.CreateDefault().WithAuthenticator(authenticator);

            spotify = new SpotifyClient(config);

            _server.Dispose();
        }
        internal void Login()
        {
            PKCEAuthenticator authenticator = new PKCEAuthenticator(Constants.SPOTIFY_CLIENT_ID, _token.Value);

            authenticator.TokenRefreshed += (_, t) =>
            {
                _token.Value = t;
                _token.Save();
                _logger.Information("Refreshed spotify token!");
            };

            SpotifyClientConfig config = SpotifyClientConfig.CreateDefault()
                                         .WithAuthenticator(authenticator);

            _spotify = new SpotifyClient(config);
        }
예제 #8
0
        public static bool StartCached(Action <SpotifyClient> onComplete)
        {
            if (!File.Exists(m_TokenPath))
            {
                return(false);
            }

            string            json  = File.ReadAllText(m_TokenPath);
            PKCETokenResponse token = JsonConvert.DeserializeObject <PKCETokenResponse>(json);

            PKCEAuthenticator authenticator = new PKCEAuthenticator(m_ClientId, token);

            authenticator.TokenRefreshed += (sender, refreshedToken) => File.WriteAllText(m_TokenPath, JsonConvert.SerializeObject(refreshedToken));

            SpotifyClientConfig config = SpotifyClientConfig.CreateDefault().WithAuthenticator(authenticator);
            SpotifyClient       client = new SpotifyClient(config);

            onComplete(client);
            return(true);
        }
예제 #9
0
        private async Task StartLoginSpotify(Stopwatch crono)
        {
            Log.Instance.PrintMessage("Logging to Spotify", MessageType.Info);
            var json = await File.ReadAllTextAsync(AuthPath);

            var token = JsonConvert.DeserializeObject <PKCETokenResponse>(json);
            var auth  = new PKCEAuthenticator(PublicKey, token);

            auth.TokenRefreshed += (sender, token) => File.WriteAllText(AuthPath, JsonConvert.SerializeObject(token));
            SpotifyConfig        = SpotifyClientConfig.CreateDefault().WithAuthenticator(auth);
            SpotifyClient        = new SpotifyClient(SpotifyConfig);
            AccountReady         = true;
            User = SpotifyClient.UserProfile.Current().Result;
            Log.Instance.PrintMessage("Connected as " + User.Email, MessageType.Correct, crono, TimeType.Seconds);
            Config.LinkedWithSpotify = true;
            AccountLinked            = true;
            Kernel.ActivarReproduccionSpotify();
            Kernel.InternetAvaliable(true);
            Kernel.BringMainFormFront();
            crono.Stop();
        }
        async void SpotifyWebAuth()
        {
            try
            {
                if (File.Exists(_path))
                {
                    var token_response = DeserializeConfig(_path, _rsaKey);

                    var authenticator = new PKCEAuthenticator(_clientID, token_response, _path);

                    var config = SpotifyClientConfig.CreateDefault()
                                 .WithAuthenticator(authenticator);
                    _spotify = new SpotifyClient(config);

                    SerializeConfig(token_response, _path, _rsaKey);


                    // This appears to be the easiest way to check if the Spotify client works, but it's not great:
                    try
                    {
                        await _spotify.Search.Item(new SearchRequest(SearchRequest.Types.Track, "fasdofimasdofiasdnfaosnf"));

                        _auth = 1;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Spotify agent dead: " + e);
                        throw new System.NullReferenceException();
                    }
                }
                else
                {
                    throw new System.NullReferenceException("Token.xml not found!");
                }
            }
            catch (System.NullReferenceException)
            {
                var(verifier, challenge) = PKCEUtil.GenerateCodes(120);

                var loginRequest = new LoginRequest(
                    new Uri("http://localhost:5000/callback"), _clientID, LoginRequest.ResponseType.Code)
                {
                    CodeChallengeMethod = "S256",
                    CodeChallenge       = challenge,
                    Scope = new[] { Scopes.UserLibraryModify, Scopes.UserFollowModify, Scopes.UserFollowRead, Scopes.UserLibraryRead }
                };
                var uri = loginRequest.ToUri();

                var server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);

                server.PkceReceived += async(sender, response) =>
                {
                    await server.Stop();

                    var initialResponse = await new OAuthClient().RequestToken(
                        new PKCETokenRequest(_clientID, response.Code, server.BaseUri, verifier)
                        );

                    //WriteOutput(initialResponse);

                    var authenticator = new PKCEAuthenticator(_clientID, initialResponse, _path);

                    var config = SpotifyClientConfig.CreateDefault()
                                 .WithAuthenticator(authenticator);
                    _spotify = new SpotifyClient(config);

                    //WriteOutput(initialResponse);
                    SerializeConfig(initialResponse, _path, _rsaKey);
                };
                await server.Start();

                try
                {
                    BrowserUtil.Open(uri);
                }
                catch (Exception)
                {
                    Console.WriteLine("Unable to open URL, manually open: {0}", uri);
                }

                _auth = 1;
            }
            catch (System.Net.WebException)
            {
                _auth = 0;
            }
            finally
            {
                mbApiInterface.MB_RefreshPanels();
                panel.Invalidate();
            }
        }