Authorising Your First Spotipy App with Client Credentials Flow

A playlist I have on Spotify has collected a good number of followers over the years, which has attracted feature requests from artists, their managements and record labels. Keeping a playlist active so your follower count keeps growing can be a bit annoying, but Spotify’s Web API can make the automation of this easy. It also lets you do lots of other interesting things too, but getting started, especially for new beginners can be a bit daunting.

Here’s how you can get started quickly using the “Client Credentials Flow” (more on that later).

Using Spotipy, a python library that lets you interact with Spotify’s Web API, we can quickly write a program that will display a given user’s public playlists.

To use Spotipy (after installing it, instructions on how in the link above) you’ll need to make an app on https://developer.spotify.com and get your client ID and client secret. We will need these for authorisation. Spotify updated their API earlier this year to bring it inline with other web APIs meaning you need to have authorisation to use it. To get publicly available data from Spotify, “Client Credentials Flow” authorisation will suffice, if you want a user’s private information, you will need to use “Authorisation Code Flow” which will prompt that user to log-in. More info on this here.

The code to retrieve a given user’s public playlists can be seen here:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

client_credentials_manager = SpotifyClientCredentials(client_id='Your_Client_ID', client_secret='Your_Client_Secret')
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

playlists = sp.user_playlists('Username')

while playlists:
    for i, playlist in enumerate(playlists['items']):
        print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'],  playlist['name']))
    if playlists['next']:
        playlists = sp.next(playlists)
    else:
        playlists = None

By passing your client_ID and client_Secret as arguments (keep them as strings) in SpotifyClientCredential() you utilise Client Credentials Flow, and by replacing playlists = sp.user_playlists('Username') ‘username’ with your username of interest, running the program will, hopefully, list that user’s public playlists.

If you want to know how to use Authorisation Code Flow, let me know and I will write a separate post. Good luck!

3 thoughts on “Authorising Your First Spotipy App with Client Credentials Flow

  1. Hi, I tried your script – it crashed in OAuth.

    It’s not clear, is the reqd username like ‘Fred Nert’, or the long string that Spotify generates.

    I have assumed that I can use the client credentials that work with mopidy, is that correct?

    Pete

    Like

    1. Hi Pete, big apologies for the late reply– I’m sorry that the script crashed. I will update the tutorial when I get the chance.
      I assume you’ve managed to get things working now but for anyone else reading this then I’ll refer you to Spotipy’s GitHub for now: https://github.com/plamere/spotipy

      Cheers
      Sotiris

      Like

Leave a Reply to Peter Diffey Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s