diff --git a/SpotifyAPI.Web/Authenticators/CredentialsAuthenticator.cs b/SpotifyAPI.Web/Authenticators/ClientCredentialsAuthenticator.cs similarity index 70% rename from SpotifyAPI.Web/Authenticators/CredentialsAuthenticator.cs rename to SpotifyAPI.Web/Authenticators/ClientCredentialsAuthenticator.cs index e6101e89..0f6d96a8 100644 --- a/SpotifyAPI.Web/Authenticators/CredentialsAuthenticator.cs +++ b/SpotifyAPI.Web/Authenticators/ClientCredentialsAuthenticator.cs @@ -7,10 +7,8 @@ namespace SpotifyAPI.Web /// 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 + public class ClientCredentialsAuthenticator : IAuthenticator { - private CredentialsTokenResponse? _token; - /// /// Initiate a new instance. The first token will be fetched when the first API call occurs /// @@ -20,15 +18,20 @@ namespace SpotifyAPI.Web /// /// The ClientID, defined in a spotify application in your Spotify Developer Dashboard /// - public CredentialsAuthenticator(string clientId, string clientSecret) + public ClientCredentialsAuthenticator(string clientId, string clientSecret) : this(clientId, clientSecret, null) { } + + public ClientCredentialsAuthenticator(string clientId, string clientSecret, ClientCredentialsTokenResponse? token) { Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId)); Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret)); ClientId = clientId; ClientSecret = clientSecret; + Token = token; } + public ClientCredentialsTokenResponse? Token { get; private set; } + /// /// The ClientID, defined in a spotify application in your Spotify Developer Dashboard /// @@ -43,13 +46,13 @@ namespace SpotifyAPI.Web { Ensure.ArgumentNotNull(request, nameof(request)); - if (_token == null || _token.IsExpired) + if (Token == null || Token.IsExpired) { var tokenRequest = new ClientCredentialsRequest(ClientId, ClientSecret); - _token = await OAuthClient.RequestToken(tokenRequest, apiConnector).ConfigureAwait(false); + Token = await OAuthClient.RequestToken(tokenRequest, apiConnector).ConfigureAwait(false); } - request.Headers["Authorization"] = $"{_token.TokenType} {_token.AccessToken}"; + request.Headers["Authorization"] = $"{Token.TokenType} {Token.AccessToken}"; } } } diff --git a/SpotifyAPI.Web/Clients/Interfaces/IOAuthClient.cs b/SpotifyAPI.Web/Clients/Interfaces/IOAuthClient.cs index 39816108..45060fc2 100644 --- a/SpotifyAPI.Web/Clients/Interfaces/IOAuthClient.cs +++ b/SpotifyAPI.Web/Clients/Interfaces/IOAuthClient.cs @@ -16,7 +16,7 @@ namespace SpotifyAPI.Web /// https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow /// /// - Task RequestToken(ClientCredentialsRequest request); + Task RequestToken(ClientCredentialsRequest request); /// /// Refresh an already received token via Authorization Code Auth diff --git a/SpotifyAPI.Web/Clients/OAuthClient.cs b/SpotifyAPI.Web/Clients/OAuthClient.cs index 8314d256..e687d461 100644 --- a/SpotifyAPI.Web/Clients/OAuthClient.cs +++ b/SpotifyAPI.Web/Clients/OAuthClient.cs @@ -24,7 +24,7 @@ namespace SpotifyAPI.Web /// https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow /// /// 1 - public Task RequestToken(ClientCredentialsRequest request) + public Task RequestToken(ClientCredentialsRequest request) { return RequestToken(request, API); } @@ -118,7 +118,7 @@ namespace SpotifyAPI.Web #pragma warning restore CA2000 } - public static Task RequestToken( + public static Task RequestToken( ClientCredentialsRequest request, IAPIConnector apiConnector ) { @@ -130,7 +130,7 @@ namespace SpotifyAPI.Web new KeyValuePair("grant_type", "client_credentials") }; - return SendOAuthRequest(apiConnector, form, request.ClientId, request.ClientSecret); + return SendOAuthRequest(apiConnector, form, request.ClientId, request.ClientSecret); } public static Task RequestToken( diff --git a/SpotifyAPI.Web/Models/Response/CredentialsTokenResponse.cs b/SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs similarity index 90% rename from SpotifyAPI.Web/Models/Response/CredentialsTokenResponse.cs rename to SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs index 1f51069b..30e00d02 100644 --- a/SpotifyAPI.Web/Models/Response/CredentialsTokenResponse.cs +++ b/SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs @@ -1,7 +1,7 @@ using System; namespace SpotifyAPI.Web { - public class CredentialsTokenResponse + public class ClientCredentialsTokenResponse { public string AccessToken { get; set; } = default!; public string TokenType { get; set; } = default!;