A .net Core wrapper for the Spotify Web Api.
Installing the package is easy.
Using the package manager console:
PM> Install-Package SpotifyWebApi-Core
Using the dotnet CLI:
> dotnet add package SpotifyWebApi-Core
var token = ClientCredentials.GetToken(new AuthParameters
{
ClientId = "clientId",
ClientSecret = "clientSecret",
Scopes = Scope.All,
});
Note: A client credentials token cannot access any personal data.
The Token
class has a usefull property to check this: token.CanAccessPersonalData
Start the authorization code process by retrieving the authentication url:
var state = Guid.NewGuid().ToString(); // Save this state because you must check it later
var parameters = new AuthParameters
{
ClientId = "clientId",
ClientSecret = "clientSecret",
RedirectUri = "https://.../callback",
Scopes = Scope.All,
ShowDialog = true
};
var url = AuthorizationCode.GetUrl(parameters, state);
At this point you should start a webserver listening on the RedirectUri
and the the client/user should login to Spotify.
The webserver should expect the following query parameters: ?code=code&state=state&error=error
// The retreived callback:
var retrievedState = "retrievedState";
var retrievedCode = "code";
var retreivedError = "";
if (state != retrievedState)
{
throw new Exception("State did not match!");
}
var token = AuthorizationCode.ProcessCallback(parameters, retrievedCode, retreivedError);
// Use the api with access to personal data.
var api = new SpotifyWebApi(token);
var me = await api.UserProfile.GetMe();
A token received with the AuthorizationCode
can also be refreshed:
// Refresh a token when its expired
if (token.IsExpired)
{
token = AuthorizationCode.RefreshToken(parameters, token);
}
After a token has been retrieved, we can use the api:
Getting a playlist and the playlist tracks:
ISpotifyWebApi api = new SpotifyWebApi(token);
var playlist = await spotify.Playlist.GetPlaylist(id);
var tracks = await spotify.Playlist.GetPlaylistTracks(id);
- Add documentation
- Add api examples
- Implement more endpoints