Exemplo n.º 1
0
        /// <summary>
        /// Checks if user is authenticated
        /// </summary>
        /// <returns>
        /// Null if failed, Profile if successful
        /// </returns>
        public static async Task <PrivateUser> IsAuthenticated()
        {
            //check if file with token exists, if it does not exist, login will be shown
            if (!File.Exists(CredentialsPath))
            {
                return(null);
            }

            var json = await File.ReadAllTextAsync(CredentialsPath);

            var token = JsonConvert.DeserializeObject <AuthorizationCodeTokenResponse>(json);

            CheckCliendSecretId();

            if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
            {
                return(null);
            }

            var authenticator = new AuthorizationCodeAuthenticator(clientId, clientSecret, token);

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

            //might throw an error if user revoked access to their spotify account
            var config = SpotifyClientConfig.CreateDefault()
                         .WithAuthenticator(authenticator);

            SpotifyClient = new SpotifyClient(config);
            //try and get user profile
            return(await SpotifyClient.UserProfile.Current());
        }
    public ISpotifyClient GetSpotifyClient()
    {
        var clientInfo = new SpotifyClientInfo(
            EnvHelpers.GetEnvOrDefault("SPOTPG_SPOTIFY_CLIENT_ID", this.configuration.ClientId),
            EnvHelpers.GetEnvOrDefault("SPOTPG_SPOTIFY_CLIENT_SECRET", this.configuration.ClientSecret));

        if (this.client != null && this.currentInfo == clientInfo)
        {
            return(this.client);
        }

        this.serviceLogger.LogInfo("Recreation Spotify client...");

        var authResponse = new AuthorizationCodeTokenResponse
        {
            AccessToken  = this.configuration.AccessToken,
            RefreshToken = this.configuration.RefreshToken
        };

        var auth = new AuthorizationCodeAuthenticator(clientInfo.Id, clientInfo.Secret, authResponse);

        var config = SpotifyClientConfig.CreateDefault()
                     .WithDefaultPaginator(new SimplePaginator())
                     .WithRetryHandler(new SimpleRetryHandler())
                     .WithAuthenticator(auth);

        this.client      = new SpotifyClient(config);
        this.currentInfo = clientInfo;

        this.serviceLogger.LogInfo("New Spotify client was created successfully");

        return(this.client);
    }
Exemplo n.º 3
0
        public async Task <ISpotifyClient> GetClientAsync(string mbUserId)
        {
            var token = await GetTokenAsync(mbUserId);

            if (token == null)
            {
                return(null);
            }

            var authenticator = new AuthorizationCodeAuthenticator(config.SpotifyClientId, config.SpotifyClientSecret, token);

            authenticator.TokenRefreshed += delegate(object o, AuthorizationCodeTokenResponse token)
            {
                // TODO: Logging via constructor - this value of log is currently null
                // log.LogInformation("Refreshing spotify token for user {user}", user);
                Task.Run(async() =>
                {
                    await SaveTokenAsync(mbUserId, token);
                }).Wait();
            };

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

            return(new SpotifyClient(spotifyConfig));
        }
Exemplo n.º 4
0
        public IAuthenticator GetAuthenticator()
        {
            var credentialsJson = File.ReadAllText(Constants.CredentialsFileName);
            var tokenResponse   = JsonConvert.DeserializeObject <AuthorizationCodeTokenResponse>(credentialsJson);

            var authenticator = new AuthorizationCodeAuthenticator(this.clientId, this.clientSecret, tokenResponse);

            authenticator.TokenRefreshed += (sender, token) => File.WriteAllText(Constants.CredentialsFileName, JsonConvert.SerializeObject(token));

            return(authenticator);
        }