In my previous post, I talked about the authorization process of the Spotify Web API. In this post, I will dig deeper into retrieving Spotify playlists.
The authorization process that I explained in my previous post is necessary for retrieving Spotify playlist data. Here’s the code that gets a list of Spotify Playlists:
First, you have to construct the Web API Uri and pass the right parameters. In this example, we call the endpoint that exposes a user’s playlists. Next, we call the generic method GetSpotifyType with two parameters: the authorization token and the Web API Endpoint URI. This method will perform the web request and convert the JSON that is returned from the Web API. There’s also a method to that retrieves all tracks from a playlist:
Likewise, we first have to define the necessary parameters for the Web API. In this case, we decide to select only 1 Spotify playlist.
Note: If you were wondering were the Playlists and Playlist classes come from, read this: As you can recall from the previous post, it is not strictly necessary to convert raw JSON data into C# classes but it is very useful and easy to do this The reusable code for these methods is the generic method GetSpotifyType:
This code simply executes a webrequest and parses the incoming data into the type that was defined. This will enable you to use the strongly typed objects in the views:
The result of this HTML looks like this:
This code that I just explained is reusable for the entire Spotify Web API, the only thing you have to be aware is the authorization. Not all data that is exposed in the API can be accessed with the Client Credentials flow.