2020-05-14 22:26:40 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Http
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// This Authenticator requests new credentials token on demand and stores them into memory.
|
|
|
|
/// It is unable to query user specifc details.
|
|
|
|
/// </summary>
|
|
|
|
public class CredentialsAuthenticator : IAuthenticator
|
|
|
|
{
|
2020-05-15 19:06:24 +01:00
|
|
|
private CredentialsTokenResponse _token;
|
2020-05-14 22:26:40 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initiate a new instance. The first token will be fetched when the first API call occurs
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="clientId">
|
|
|
|
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
|
|
|
|
/// </param>
|
|
|
|
/// <param name="clientSecret">
|
|
|
|
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
|
|
|
|
/// </param>
|
|
|
|
public CredentialsAuthenticator(string clientId, string clientSecret)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
|
|
|
|
Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret));
|
|
|
|
|
|
|
|
ClientId = clientId;
|
|
|
|
ClientSecret = clientSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
|
|
|
|
/// </summary>
|
|
|
|
public string ClientId { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
|
|
|
|
/// </summary>
|
2020-05-15 19:06:24 +01:00
|
|
|
public string ClientSecret { get; }
|
2020-05-14 22:26:40 +01:00
|
|
|
|
|
|
|
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}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|