plamere / pbl

a python library for building playlists
MIT License
45 stars 10 forks source link

Latest commits breaking authenticated sessions' friendliness ? #2

Open myselfhimself opened 8 years ago

myselfhimself commented 8 years ago

Hello,

I would like to fetch things from a private playlist and post it to another user's private playlist.

I stumble upon an error for the first fetch.. with current Github's snapshot.

spotipy.client.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/users/some_user/playlists/5yBknOq8Eo0oJe1XCARNaT/tracks:
 This request requires user authentication.

It seems like that after 1.1.5, the _get_spotify() does not care anymore about the current user name and relies on engine.getEnv()'s "auth_token" value. So, do I have to get a token for my user in some thread, store it with engine.setEnv(), then create another thread for the user and store it the same way ? Do you have example pieces of code for this ? That prompt_user_token() wizard, with use of .cache-username method from Spotipy was very practical before...

Here is a piece of code for the 1.1.5 version that rewrite _get_auth_spotify(user) for multi-sessions:

def _get_auth_spotify(user):
    global auth_sp
    if auth_sp == None or user not in auth_sp:
        scope = 'playlist-modify-public playlist-modify-private'
        token = spotipy.util.prompt_for_user_token(user, scope)
        if token:
            auth_sp = {}
            auth_sp[user] = spotipy.Spotify(auth=token)

    return auth_sp[user]

Thanks for your help in advance!

myselfhimself commented 8 years ago

I cannot do a pull request again your master branch, but pushed a commit onto a public fork that undoes your last master commit first. See that other forked branch's commit right above this message. Maybe with time, I will take your new code with redis support, and integrate multi-sessions inside.