using System.Threading.Tasks; namespace SpotifyAPI.Web.Http { /// /// This Authenticator requests new credentials token on demand and stores them into memory. /// It is unable to query user specifc details. /// public class CredentialsAuthenticator : IAuthenticator { private CredentialsTokenResponse? _token; /// /// Initiate a new instance. The first token will be fetched when the first API call occurs /// /// /// The ClientID, defined in a spotify application in your Spotify Developer Dashboard /// /// /// The ClientID, defined in a spotify application in your Spotify Developer Dashboard /// public CredentialsAuthenticator(string clientId, string clientSecret) { Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId)); Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret)); ClientId = clientId; ClientSecret = clientSecret; } /// /// The ClientID, defined in a spotify application in your Spotify Developer Dashboard /// public string ClientId { get; } /// /// The ClientID, defined in a spotify application in your Spotify Developer Dashboard /// public string ClientSecret { get; } public async Task Apply(IRequest request, IAPIConnector apiConnector) { Ensure.ArgumentNotNull(request, nameof(request)); if (_token == null || _token.IsExpired) { var tokenRequest = new ClientCredentialsRequest(ClientId, ClientSecret); _token = await OAuthClient.RequestToken(tokenRequest, apiConnector).ConfigureAwait(false); } request.Headers["Authorization"] = $"{_token.TokenType} {_token.AccessToken}"; } } }