[GH-ISSUE #749] HTTP 401 for playlist_upload_cover_image #454

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

Originally created by @valentinfrlch on GitHub (Nov 29, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/749

So I have a function that takes an image, converts it into base64 and then makes the PUT request through the built in spotify.playlist_upload_cover_image() function. However I always get a 401. I have already refreshed the token - to no avail. It might also be the scope, but I've tripple-checked ugc-image-upload should be right for uploading a custom playlist cover. What am I doing wrong?

Here's the code:

def upload_cover(playlist_id):
scope = "ugc-image-upload"
spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri))
image = open("dataset/thumbnail.jpg", 'rb')
image_read = image.read()
cover_encoded = base64.encodebytes(image_read).strip()
spotify.playlist_upload_cover_image(playlist_id, cover_encoded)

And here's the output:

headers=response.headers,
spotipy.exceptions.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/playlists/playlist_id/images:
Unauthorized., reason: None

Originally created by @valentinfrlch on GitHub (Nov 29, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/749 So I have a function that takes an image, converts it into base64 and then makes the PUT request through the built in spotify.playlist_upload_cover_image() function. However I always get a 401. I have already refreshed the token - to no avail. It might also be the scope, but I've tripple-checked `ugc-image-upload` should be right for uploading a custom playlist cover. What am I doing wrong? Here's the code: def upload_cover(playlist_id): scope = "ugc-image-upload" spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)) image = open("dataset/thumbnail.jpg", 'rb') image_read = image.read() cover_encoded = base64.encodebytes(image_read).strip() spotify.playlist_upload_cover_image(playlist_id, cover_encoded) And here's the output: headers=response.headers, spotipy.exceptions.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/playlists/playlist_id/images: Unauthorized., reason: None
kerem 2026-02-27 23:22:43 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@stephanebruckert commented on GitHub (Nov 29, 2021):

Check out the tests to see how it can be done

github.com/plamere/spotipy@48d04f343b/tests/integration/test_user_endpoints.py (L183)

I see it's using base64.b64encode, not base64.encodebytes
github.com/plamere/spotipy@48d04f343b/tests/helpers.py (L15)

<!-- gh-comment-id:981926935 --> @stephanebruckert commented on GitHub (Nov 29, 2021): Check out the tests to see how it can be done https://github.com/plamere/spotipy/blob/48d04f343b99014822a87d6ec4965c6c921be8ac/tests/integration/test_user_endpoints.py#L183 I see it's using `base64.b64encode`, not `base64.encodebytes` https://github.com/plamere/spotipy/blob/48d04f343b99014822a87d6ec4965c6c921be8ac/tests/helpers.py#L15
Author
Owner

@valentinfrlch commented on GitHub (Nov 29, 2021):

the .decode("utf-8") at the end did the trick!

<!-- gh-comment-id:981957093 --> @valentinfrlch commented on GitHub (Nov 29, 2021): the .decode("utf-8") at the end did the trick!
Author
Owner

@valentinfrlch commented on GitHub (Dec 8, 2021):

For some weird reason the suggested solution has only worked once and never since. I have updated my code to the following:
with open("dataset/thumbnail.jpg", 'rb') as image: cover_encoded = base64.b64encode(image.read()).decode("utf-8") spotify.playlist_upload_cover_image(playlist_id, cover_encoded)

The only way my code is different from the one in user_endpoint_tests.py is that I read and encode a local file. I don't think the problem is with the way I encode the file, as I have successfully encoded and the decoded the file.
Also I have tried to run the test, and I could get it to pass.
The scope is "ugc-image-upload"

<!-- gh-comment-id:989125267 --> @valentinfrlch commented on GitHub (Dec 8, 2021): For some weird reason the suggested solution has only worked once and never since. I have updated my code to the following: ` with open("dataset/thumbnail.jpg", 'rb') as image: cover_encoded = base64.b64encode(image.read()).decode("utf-8") spotify.playlist_upload_cover_image(playlist_id, cover_encoded)` The only way my code is different from the one in user_endpoint_tests.py is that I read and encode a local file. I don't think the problem is with the way I encode the file, as I have successfully encoded and the decoded the file. Also I have tried to run the test, and I could get it to pass. The scope is `"ugc-image-upload"`
Author
Owner

@stephanebruckert commented on GitHub (Dec 8, 2021):

If the issue occurs on the playlist_upload_cover_image call, make sure:

  • the image is initially in the JPEG format
  • the image size is maximum 256 KB

It might have worked on a smaller image before.

If this doesn't resolve your issue, please share a complete minimal example with the image you are using, and I will try to help. Thanks!

<!-- gh-comment-id:989180599 --> @stephanebruckert commented on GitHub (Dec 8, 2021): If the issue occurs on the `playlist_upload_cover_image` call, make sure: - the image is initially in the JPEG format - the image size is maximum 256 KB It might have worked on a smaller image before. If this doesn't resolve your issue, please share a complete minimal example with the image you are using, and I will try to help. Thanks!
Author
Owner

@valentinfrlch commented on GitHub (Dec 9, 2021):

Thanks for your reply. The image I‘ve tested this with is 80Kb and in JPEG format. I have attatched it below. Here‘s my code:

def upload_cover(playlist_id):
scope = "ugc-image-upload"
spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(
scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri))
# reformat encoding jpg -> base64
with open("dataset/thumbnail.jpg", 'rb') as image:
cover_encoded = base64.b64encode(image.read()).decode("utf-8")
spotify.playlist_upload_cover_image(playlist_id, cover_encoded)

thumbnail

<!-- gh-comment-id:989569833 --> @valentinfrlch commented on GitHub (Dec 9, 2021): Thanks for your reply. The image I‘ve tested this with is 80Kb and in JPEG format. I have attatched it below. Here‘s my code: def upload_cover(playlist_id): scope = "ugc-image-upload" spotify = spotipy.Spotify(auth_manager=SpotifyOAuth( scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)) # reformat encoding jpg -> base64 with open("dataset/thumbnail.jpg", 'rb') as image: cover_encoded = base64.b64encode(image.read()).decode("utf-8") spotify.playlist_upload_cover_image(playlist_id, cover_encoded) ![thumbnail](https://user-images.githubusercontent.com/85313672/145348459-8a014bb2-047d-42a8-a7c5-519e0e45f337.jpg)
Author
Owner

@stephanebruckert commented on GitHub (Dec 10, 2021):

I also get 401, it's strange. It could be linked to https://community.spotify.com/t5/Spotify-for-Developers/Bug-Uploading-cover-image-not-working-with-un-refreshed-access/m-p/5156729#M2056 which would be a bug on the Spotify side, but so far I can't reproduce the suggested solution.

<!-- gh-comment-id:991324666 --> @stephanebruckert commented on GitHub (Dec 10, 2021): I also get 401, it's strange. It could be linked to https://community.spotify.com/t5/Spotify-for-Developers/Bug-Uploading-cover-image-not-working-with-un-refreshed-access/m-p/5156729#M2056 which would be a bug on the Spotify side, but so far I can't reproduce the suggested solution.
Author
Owner

@valentinfrlch commented on GitHub (Dec 13, 2021):

Thank you very much for your reply and also for linking the other thread. Yes it is very strange. Let's hope spotify fixes this soon. Am I right that it does work with a refreshed token? If so, how would I implement this?

<!-- gh-comment-id:992685411 --> @valentinfrlch commented on GitHub (Dec 13, 2021): Thank you very much for your reply and also for linking the other thread. Yes it is very strange. Let's hope spotify fixes this soon. Am I right that it does work with a refreshed token? If so, how would I implement this?
Author
Owner

@stephanebruckert commented on GitHub (Jan 21, 2022):

@valentinfrlch did you fix it?

<!-- gh-comment-id:1018324398 --> @stephanebruckert commented on GitHub (Jan 21, 2022): @valentinfrlch did you fix it?
Author
Owner

@valentinfrlch commented on GitHub (Jan 21, 2022):

@valentinfrlch did you fix it?

No unfortunately not. But I guess all we can really do is to wait for Spotify to fix it, since it's a problem on their end.

<!-- gh-comment-id:1018502830 --> @valentinfrlch commented on GitHub (Jan 21, 2022): > @valentinfrlch did you fix it? No unfortunately not. But I guess all we can really do is to wait for Spotify to fix it, since it's a problem on their end.
Author
Owner

@stephanebruckert commented on GitHub (Jan 21, 2022):

I have some live code that is running playlist_upload_cover_image smoothly, uploading covers correctly...

github.com/mirrorfm/mirrorfm-core@020e7a2eb3/functions/to-spotify/main.py (L276)


It looks identical to the code I tried locally, but what I can do is compare the python/spotipy versions. I'll try to do this ASAP, lacking a bit of time at the moment

<!-- gh-comment-id:1018510178 --> @stephanebruckert commented on GitHub (Jan 21, 2022): I have some live code that is running `playlist_upload_cover_image` smoothly, uploading covers correctly... https://github.com/mirrorfm/mirrorfm-core/blob/020e7a2eb3e2416c73bfb4ed7f71681a35a23fe6/functions/to-spotify/main.py#L276 ----- It looks identical to the code I tried locally, but what I can do is compare the python/spotipy versions. I'll try to do this ASAP, lacking a bit of time at the moment
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#454
No description provided.