Search code examples
pythonapispotifyput

How to play a song from a specified playlist (Spotify API)


I want to play all the songs of a given playlist using the spotify API in python. I have got the access token and the required stuff, but am getting a response of 403, the first method find_songs returns my playlist and it returns a response of 200, but not the second method

class SaveSongs:
def __init__(self):
    self.user_id = spotify_user_id
    self.spotify_token = ""
    self.playlist_id = playlist_id
    self.tracks = ""
    self.new_playlist_id = ""
    self.device_id = "DESKTOP-9UBFPPN"

def find_songs(self):
    print("Finding songs in your playlist...")
    query = "https://api.spotify.com/v1/playlists/{}/tracks".format(
        playlist_id)
    response = requests.get(query,
                            headers={"Content-Type": "application/json",
                                     "Authorization": "Bearer {}".format(self.spotify_token)})
    response_json = response.json()
    print(response)

def call_refresh(self):
    print("Refreshing token")
    refreshCaller = Refresh()
    self.spotify_token = refreshCaller.refresh()
    self.find_songs()

def playSong(self): #issue here
    query = "https://api.spotify.com/v1/me/player/play"
    response = requests.put(query,
                            headers={"Content-Type": "application/json",
                                    "Authorization": "Bearer {}".format(self.spotify_token)}, data={
        "context_uri": "spotify:playlist:7mnkglhHfzZRYFRG72zCMd"
    })

    print(response)

a = SaveSongs()
a.call_refresh()
a.playSong()

Refresh.py:

class Refresh:

def __init__(self):
    self.refresh_token = refresh_token
    self.base_64 = base_64

def refresh(self):

    query = "https://accounts.spotify.com/api/token"

    response = requests.post(query,
                             data={"grant_type": "refresh_token",
                                   "refresh_token": refresh_token},
                             headers={"Authorization": "Basic " + base_64})

    response_json = response.json()
    print(response_json)

    return response_json["access_token"]


a = Refresh()
a.refresh()

cURL command

curl -H "Authorization: Basic " -d grant_type=authorization_code -d code= -d redirect_uri=https%3A%2F%2Fgithub.com%2FRohith-JN https://accounts.spotify.com/api/token

get request to get the code for curl command:

https://accounts.spotify.com/authorize?client_id=&response_type=code&redirect_uri=https%3A%2F%2Fgithub.com%2FRohith-JN&scope=playlist-modify-public%20playlist-modify-private%20user-modify-playback-state

expired access token:

BQDbYxNY8h2Crof2_aE1-Tzgk0fnQ5EHBjenqM1dmExNpmbEaw5Ra548kEM86vSLeZ9XTuHLgRXRxxgLQF1aXcqcpUivuaEBjQSmjRFfeBGazCdVxSZQgCMDR_XRa5PhhrVebE2gYfDQl468BkSEGSGWV5LJ2EOYbWS5fsCTHWt-XtM7TFEDQJw3J43yiYf98pBWggp7CQM_yv31XLjoNe93A-j-rwQEJQ0

Solution

  • I figured out that the problem was not with my code, For some calls such as play/pause, increase or decrease volume require a Spotify premium account.