Home > database >  Spotipy ERROR 401 - Unauthorized although the used token is valid and already used before
Spotipy ERROR 401 - Unauthorized although the used token is valid and already used before

Time:07-02

Heyo Guys,
I am kinda frustraded because I can't find the error. I try to setup a script, which sets my playlists details back to how it was because of report abuses. I am using 'playlist_change_details' and 'playlist_upload_cover_image' which should use the same scope. Changing the name and the description is working, but the cover image tells me it got no token provided, although they use the same requested token. The image itself is valid and I didn't get an error about that.

Authentication Part (TaskSpotifyAuth.py):

import spotipy
import spotipy.oauth2 as oauth2

def auth(SCOPE = None, debug = False):
if SCOPE != None:
    sp_oauth = oauth2.SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET, scope=SCOPE,
                                   redirect_uri=SPOTIPY_REDIRECT_URI, cache_path=CACHE)
    access_token = ""
    token_info = sp_oauth.get_cached_token()
    if token_info != None:
        if token_info['scope'] == SCOPE and sp_oauth.validate_token(token_info):
            print("Found cached token for the scope: <"   SCOPE   ">") if debug == True else None
            access_token = token_info['access_token']
    else:
        url = sp_oauth.get_authorization_code()
        print(url)
        code = sp_oauth.parse_response_code(url)
        if code != "":
            print("Found Spotify auth code in Request URL! Trying to get valid access token...") if debug == True else None
            token_info = sp_oauth.get_access_token(code=code)
            access_token = token_info['access_token']
    if access_token:
        print("Access token available! Trying to get user information...") if debug == True else None
        sp = spotipy.Spotify(access_token)
        return sp
    else:
        return None

Changing Details Part (TaskSpotify.py):

import TaskSpotifyAuth
def SpotifyLogin(scope=None, debug=False):
if scope == None:
    return TaskSpotifyAuth.noPreToken()
else:
    return TaskSpotifyAuth.auth(SCOPE=scope, debug=debug)

def change_playlist_details(pl_id, pl_name, pl_desc, pl_cover, debug=False):
sp = SpotifyLogin(scope='playlist-modify-public', debug=debug)
try:
    sp.playlist_change_details(playlist_id=pl_id, name=pl_name, description=pl_desc)
    try:
        sp.playlist_upload_cover_image(playlist_id=pl_id, image_b64=pl_cover)
    except:
        print('Uploading a new cover image failed. The details of the playlist got changed.')
except:
    print('Changing of the playlist details failed. No changes were made.')

Console Output:
#XXXXXXXXXXXXXX
Found Spotify auth code in Request URL! Trying to get valid access token...
Access token available! Trying to get user information...
HTTP Error for PUT to https://api.spotify.com/v1/playlists/XXXXXXXXXXXXXX/images with Params: {} returned 401 due to Unauthorized.
Uploading a new cover image failed. The details of the playlist got changed.

Message in the HTTP Error:

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

Thank you for your time!

Greets
MrSchiller

CodePudding user response:

Looking at the documentation for authorization scopes, you may also need to include the scope for ugc-image-upload.

  • Related