[GH-ISSUE #774] Getting invalid context URI when passing variable containing URI to param for start_playback() #474

Open
opened 2026-02-27 23:22:50 +03:00 by kerem · 10 comments
Owner

Originally created by @StanleyDharan on GitHub (Jan 24, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/774

I am reading the ID part of the spotify album URI from an external source and concatenating it to a base URI, doing this will cause an invalid context uri, reason: none error
example:

albumURI = 'spotify:album:'
albumURI += data.decode('utf-8')
sp.start_playback(device_id='***', context_uri=albumURI)

but if I hard code the variable albumURI like this: albumURI = 'spotify:album:6QC8G4HVk9lkbpxugU7ZgF' it will work... (to be exact, I could do print(albumURI) copy the output and hard code albumURI with it and the function will execute)

Im really confused why this happening, data is a UFT-8 byteArray. I am also fairly new to python so if I am missing some string clean-up or something when decoding please let me know.

This is the exact error I am getting:

HTTP Error for PUT to https://api.spotify.com/v1/me/player/play?device_id=*** with Params: {} returned 400 due to Invalid context uri
http status: 400, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=***:
 Invalid context uri, reason: None

*** = my actual device ID
Any guidance would be greatly appreciated.

Originally created by @StanleyDharan on GitHub (Jan 24, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/774 <!--- Please make sure you've: - read the FAQ https://github.com/plamere/spotipy/blob/master/FAQ.md - read the documentation https://spotipy.readthedocs.io/en/latest/ - searched older issues If your question is about code, please share the code you are using ---> I am reading the ID part of the spotify album URI from an external source and concatenating it to a base URI, doing this will cause an `invalid context uri, reason: none` error example: ``` albumURI = 'spotify:album:' albumURI += data.decode('utf-8') sp.start_playback(device_id='***', context_uri=albumURI) ``` but if I hard code the variable `albumURI` like this: `albumURI = 'spotify:album:6QC8G4HVk9lkbpxugU7ZgF'` it will work... (to be exact, I could do `print(albumURI)` copy the output and hard code `albumURI` with it and the function will execute) Im really confused why this happening, `data` is a UFT-8 byteArray. I am also fairly new to python so if I am missing some string clean-up or something when decoding please let me know. This is the exact error I am getting: ``` HTTP Error for PUT to https://api.spotify.com/v1/me/player/play?device_id=*** with Params: {} returned 400 due to Invalid context uri http status: 400, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=***: Invalid context uri, reason: None ``` *** = my actual device ID Any guidance would be greatly appreciated.
Author
Owner

@Peter-Schorn commented on GitHub (Jan 24, 2022):

There is no such thing as a "base URI". Post the value of data.

<!-- gh-comment-id:1019887791 --> @Peter-Schorn commented on GitHub (Jan 24, 2022): There is no such thing as a "base URI". Post the value of `data`.
Author
Owner

@StanleyDharan commented on GitHub (Jan 24, 2022):

There is no such thing as a "base URI". Post the value of data.

data a byteArray, its 16 bytes long, I decode() it into a string and concatenate it to albumURI = 'spotify:album:'

at max there will be 3 16 byte long byteArrays that are decode() and concatenated to albumURI

<!-- gh-comment-id:1020323022 --> @StanleyDharan commented on GitHub (Jan 24, 2022): > There is no such thing as a "base URI". Post the value of `data`. `data` a byteArray, its 16 bytes long, I `decode()` it into a string and concatenate it to `albumURI = 'spotify:album:'` at max there will be 3 16 byte long byteArrays that are `decode()` and concatenated to `albumURI`
Author
Owner

@Peter-Schorn commented on GitHub (Jan 24, 2022):

How could anyone solve your issue if you don't post the value of data? If it contains a utf-8 encoded album id, then the code will work; if not, then you will get a "Invalid context uri" error.

<!-- gh-comment-id:1020387371 --> @Peter-Schorn commented on GitHub (Jan 24, 2022): How could anyone solve your issue if you don't post the value of `data`? If it contains a utf-8 encoded album id, then the code will work; if not, then you will get a "Invalid context uri" error.
Author
Owner

@StanleyDharan commented on GitHub (Jan 24, 2022):

Sorry, this is the content of data for the example I mentioned above;

data: bytearray(b'6QC8G4HVk9lkbpxu')
data: bytearray(b'gU7ZgF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
data: bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f')

The reason data is printed 3 times is because I am looping through an array of ByteArrays

<!-- gh-comment-id:1020397791 --> @StanleyDharan commented on GitHub (Jan 24, 2022): Sorry, this is the content of data for the example I mentioned above; ``` data: bytearray(b'6QC8G4HVk9lkbpxu') data: bytearray(b'gU7ZgF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') data: bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f') ``` The reason `data` is printed 3 times is because I am looping through an `array` of `ByteArrays`
Author
Owner

@Peter-Schorn commented on GitHub (Jan 24, 2022):

If you decode these data values into strings and print them (why didn't you think of doing this?), it should be obvious why your code doesn't work:

data1 = bytearray(b'6QC8G4HVk9lkbpxu')
data2 = bytearray(b'gU7ZgF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
data3 = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f')

for data in [data1, data2, data3]:
    string = data.decode('utf-8')
    print(string)

output:

6QC8G4HVk9lkbpxu
gU7ZgF����������
�	

\x00 denotes the null character, which obviously shouldn't be present in a Spotify Id. Spotify Ids will always be alphanumeric characters. In fact, most of the characters in your byte arrays are non-printing control characters.

<!-- gh-comment-id:1020418865 --> @Peter-Schorn commented on GitHub (Jan 24, 2022): If you decode these `data` values into strings and print them (why didn't you think of doing this?), it should be obvious why your code doesn't work: ```python data1 = bytearray(b'6QC8G4HVk9lkbpxu') data2 = bytearray(b'gU7ZgF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') data3 = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f') for data in [data1, data2, data3]: string = data.decode('utf-8') print(string) ``` output: ``` 6QC8G4HVk9lkbpxu gU7ZgF���������� � ``` `\x00` denotes the null character, which obviously shouldn't be present in a Spotify Id. Spotify Ids will always be alphanumeric characters. In fact, most of the characters in your byte arrays are non-printing control characters.
Author
Owner

@StanleyDharan commented on GitHub (Jan 24, 2022):

Thanks for all your help Peter! I solved the issue you laid out with this little filter function:

decodedURI = data.decode('utf-8')
# Stripping non-alphanumeric characters
filtered_uri = "".join(filter(lambda x: x in string.printable, decodedURI))
<!-- gh-comment-id:1020511429 --> @StanleyDharan commented on GitHub (Jan 24, 2022): Thanks for all your help Peter! I solved the issue you laid out with this little filter function: ``` decodedURI = data.decode('utf-8') # Stripping non-alphanumeric characters filtered_uri = "".join(filter(lambda x: x in string.printable, decodedURI)) ```
Author
Owner

@Peter-Schorn commented on GitHub (Jan 24, 2022):

This is not sufficient because if you remove non-alphanumeric characters, then the string is not long enough to be a Spotify id, at least for the data objects you posted.

<!-- gh-comment-id:1020516752 --> @Peter-Schorn commented on GitHub (Jan 24, 2022): This is not sufficient because if you remove non-alphanumeric characters, then the string is not long enough to be a Spotify id, at least for the data objects you posted.
Author
Owner

@StanleyDharan commented on GitHub (Jan 24, 2022):

So the data object after all the manipulation is done is appended to this string: albumURI = 'spotify:album:'

ie.

albumURI += filtered_uri

and that is then sent to sp.start_playback(device_id='***', context_uri=albumURI) and its been working great :)

<!-- gh-comment-id:1020518742 --> @StanleyDharan commented on GitHub (Jan 24, 2022): So the data object after all the manipulation is done is appended to this string: `albumURI = 'spotify:album:'` ie. ``` albumURI += filtered_uri ``` and that is then sent to `sp.start_playback(device_id='***', context_uri=albumURI)` and its been working great :)
Author
Owner

@Peter-Schorn commented on GitHub (Jan 24, 2022):

But gU7ZgF is not a valid Spotify id, and your last byte array (bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f')) doesn't contain any alphanumeric characters.

<!-- gh-comment-id:1020521767 --> @Peter-Schorn commented on GitHub (Jan 24, 2022): But `gU7ZgF` is not a valid Spotify id, and your last byte array (`bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f')`) doesn't contain any alphanumeric characters.
Author
Owner

@StanleyDharan commented on GitHub (Jan 24, 2022):

Oh sorry let me clarify with the full back story of what I am doing.

Short answer:
In your above comment where you listed out data1-3, those 3 data# variables are read, stripped and concatenated together with albumURI

Long answer:
The reason i'm going through this process is because of a hardware limitation; I am reading the album ID from an mifare nfc card. The limitation is that; MiFare classic NFC cards have a limited # of data blocks to hold whatever data you want, each data block can contain 16 bytes, and must always contain 16 bytes of data. So because the album ID is longer then 16 I break it up over multiple data blocks BUT since the album ID isn't always long enough to fill 2 whole data blocks, I add in filler bytes

Im making this (watch the tiktok) https://www.tiktok.com/@talaexe/video/7051357248739659013?lang=en&is_copy_url=0&is_from_webapp=v1&sender_device=pc&sender_web_id=6925974970854508038

<!-- gh-comment-id:1020527720 --> @StanleyDharan commented on GitHub (Jan 24, 2022): Oh sorry let me clarify with the full back story of what I am doing. Short answer: In your above comment where you listed out data1-3, those 3 data# variables are read, stripped and concatenated together with `albumURI` Long answer: The reason i'm going through this process is because of a hardware limitation; I am reading the album ID from an mifare nfc card. The limitation is that; MiFare classic NFC cards have a limited # of data blocks to hold whatever data you want, each data block can contain 16 bytes, and must always contain 16 bytes of data. So because the album ID is longer then 16 I break it up over multiple data blocks BUT since the album ID isn't always long enough to fill 2 whole data blocks, I add in filler bytes Im making this (watch the tiktok) https://www.tiktok.com/@talaexe/video/7051357248739659013?lang=en&is_copy_url=0&is_from_webapp=v1&sender_device=pc&sender_web_id=6925974970854508038
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#474
No description provided.