Beispiel #1
0
    private async void OnAuthenticatorComplete(object authObject)
    {
        if (authObject != null)
        {
            // Get config from authenticator
            if (authObject is IAuthenticator apiAuthenticator)
            {
                _defaultConfig = SpotifyClientConfig.CreateDefault().WithAuthenticator(apiAuthenticator);
            }
            else if (authObject is string authToken)
            {
                _defaultConfig = SpotifyClientConfig.CreateDefault().WithToken(authToken);
            }
            else
            {
                Debug.LogError("Auth complete object is unknown. Authentification failed.");
                return;
            }

            // Create the Spotify client
            _client = new SpotifyClient(_defaultConfig);

            if (_client != null)
            {
                // Make one test api request to validate/refresh auth
                await SendValidationRequest();

                Action clientCompleteAction = () =>
                {
                    OnClientConnectionChanged?.Invoke(_client);
                    Debug.Log($"Successfully connected to Spotify using '{AuthType}' authentificiation");
                };

                // If authenticator completed on another thread, add event to dispatcher to run on main thread
                if (Thread.CurrentThread.IsBackground)
                {
                    _dispatcher.Add(clientCompleteAction);
                }
                else
                {
                    // Is main thread, invoke now
                    clientCompleteAction.Invoke();
                }
            }
            else
            {
                Debug.LogError("Unknown error creating SpotifyAPI client");
            }
        }
        else
        {
            Debug.LogError($"Authenticator '{AuthType}' is complete but not provided a valid authenticator");
        }
    }
Beispiel #2
0
    /// <summary>
    /// Signs the current user out. Requires AuthorizeUser() to authorize again.
    /// </summary>
    /// <param name="removeSavedAuth">Should any saved auth be removed</param>
    public void DeauthorizeUser(bool removeSavedAuth = false)
    {
        if (_client != null)
        {
            _client = null;
        }

        if (_authenticator != null)
        {
            _authenticator.DeauthorizeUser();

            if (removeSavedAuth)
            {
                _authenticator.RemoveSavedAuth();
            }
        }

        // Client no longer connected
        OnClientConnectionChanged?.Invoke(_client);
    }