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.
This would be a lot easier if you provided the code for the Item and Playlist classes. I know it’s not necessary to use them, but you do, therefore the code would be helpful for anyone following this tutorial.
Hi Timothy, thanks for the feedback! I’ll provide the code as soon as I can.
Have you posted the Item and Playlist classes somewhere? 🙂
Have you uploaded the code anywhere yet? Kind of stuck on how I can get a list of all the playlists? (Running into a lot of null pointer exceptions)
Have you posted the two classes somewhere? 🙂
Hi, have you posted the Item and Playlist classes somewhere? 🙂
Jacob, I placed them in the gist for you. I just c/p’d in the json to a json to c# converter and it created them for me. I’m not sure if you need all 3 externalurls, but it works like this. Hope this helps!