Search code examples
pythonpython-3.xspotifyspotipy

Error 403 when trying to control playback on another device through spotipy library (Spotify Web API)


Describe the bug

I am getting the 403 error whenever I try to start playback on a Spotify device.

I am attempting playback on my Google Home Mini speaker (using another library called pychromecast but that is working) with this code client.start_playback(device_id=spotify_device_id, context_uri=tag_uri) on line 197.

The 403 error is usually when the user does not have Premium (I think). I have Premium though and have double-checked. The authorization works fine.

My code

https://pastebin.com/fha4Rbdh

Expected behavior

  • Read Spotify album URI from an NFC tag
  • Start playback of that album on a Spotify device (my Google Home Mini)

Actual behavior

  • Reads Spotify album URI from an NFC tag
  • Attempts to start playback on Spotify device
  • Fails with 403 error

Output

https://pastebin.com/Ejizn084

Environment:

  • OS: Raspberry Pi 3
  • Python version 3.5.3
  • spotipy version 2.13.0

Additional context

My code worked on Windows 10, then after being transferred to my pi it has no longer worked. I thought it was not authorizing correctly (it wasn't) so I fixed that, but I am still getting this same error.

I think this error might have something to do with the scope I am using, which is:

_scope = 'streaming user-read-playback-state user-modify-playback-state'

Please help!


Solution

  • I had this problem a while ago now, but if I remember correctly, I had been setting the Spotify URIs to lowercase, but Spotify URIs are case-sensitive. So when I made requests to start playback of those URIs, Spotify didn't understand some of them but understood others (URIs that had no uppercase letters). That explains why it worked on my laptop, but not on the raspberry pi. I had used different URIs on each (not very scientific).

    It took me 3 months of on and off debugging to solve that one. Sometimes it is the smallest change that fixes the biggest problem.