Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

API retrieving my data not user data

API retrieving my data not user data

My Question or Issue

 

I created a small app. A part of it displays a list of a user's playlists for them to select from. First I had issues with users being able to log in again as I had a cache folder in my git repository that was pushing old tokens. I deleted that cache file along with the pycache folder. Since then, when a user is taken to the page to select one of their playlists, it is showing my personal spotify playlists rather than theirs! I have no idea what has happened! I haven't altered any code in the app.py file. About an hour ago, when a user first logged in, it was showing their playlists. Until I deleted the cache files. Any clues on what could be the issue? 

Reply
7 Replies

Without any code provided, nobody can tell.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Here is the code for the relevant pages:

 

@app.route('/toptracks')
def redirector():
    sp_oauth= create_spotify_oauth()
    session.clear()
    code= request.args.get('code')
    token_info= sp_oauth.get_access_token(code)
    print(token_info)
    session[TOKEN_INFO]= token_info
    return redirect('/selectplaylist')

@app.route('/selectplaylist')
def selectPlaylist():
    try:
        token_info= get_token()
    except:
        print('try logging in again')
        redirect('/')
    sp= spotipy.Spotify(auth=token_info['access_token'])
    user= sp.current_user()['id']
    allPlaylists= sp.user_playlists(user=user,limit=50)['items']
    playName= [[i.get('name'),i.get('id')] for i in allPlaylists]
    playId= [i.get('id') for i in allPlaylists]
    return render_template('selectPlaylist.html',playName=playName,playId=playId)

def get_token():
    token_info= session.get(TOKEN_INFO, None)
    if not token_info:
        raise 'exception'
    now = int(time.time())
    is_expired= token_info['expires_at'] - now < 60
    if (is_expired):
        sp_oauth= create_spotify_oauth()
        token_info= sp_oauth.refresh_access_token(token_info['refresh_token'])
    return token_info

@app.route('/toptracks')
def redirector():
    sp_oauth= create_spotify_oauth()
    session.clear()
    code= request.args.get('code')
    token_info= sp_oauth.get_access_token(code)
    print(token_info)
    session[TOKEN_INFO]= token_info
    return redirect('/selectplaylist')

@app.route('/selectplaylist')
def selectPlaylist():
    try:
        token_info= get_token()
    except:
        print('try logging in again')
        redirect('/')
    sp= spotipy.Spotify(auth=token_info['access_token'])
    user= sp.current_user()['id']
    allPlaylists= sp.user_playlists(user=user,limit=50)['items']
    playName= [[i.get('name'),i.get('id')] for i in allPlaylists]
    playId= [i.get('id') for i in allPlaylists]
    return render_template('selectPlaylist.html',playName=playName,playId=playId)

def get_token():
    token_info= session.get(TOKEN_INFO, None)
    if not token_info:
        raise 'exception'
    now = int(time.time())
    is_expired= token_info['expires_at'] - now < 60
    if (is_expired):
        sp_oauth= create_spotify_oauth()
        token_info= sp_oauth.refresh_access_token(token_info['refresh_token'])
    return token_info

https://stackoverflow.com/questions/76534799/why-is-spotify-api-displaying-my-data-to-other-users

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Wish that wasn't my question, but it is my own post ๐Ÿ˜…

Get a solution to this? Experiencing the same problem.

I also had problems with the .cache file that Spotify created in my directory. It then always used old tokens still stored in there, which made my software request to the account with that old token, even tho I OAuth with another account.
The flask session.clear() does not remove the .cache. Don't try to manually delete content from .cache and leave it their. This will raise some json errors from libraries used by Spotipy. I made sure to delete the .cache enterly at appropriate places in the code. The .cache will be build by Spotipy with the new token automatically.
"""

def cleanup():
    cache_file = '.cache'
    if os.path.exists(cache_file):
        os.remove(cache_file)
"""
I'm calling this when I exit my software and basically always, when I also do session.clear().
So try calling cleanup() at places where you also clear the session.

Suggested posts

Staff
Let's introduce ourselves!

Hey there you,   Yeah, you! ๐Ÿ˜   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and getโ€ฆ

Staff
ModeratorStaff / Moderator/ 3 years ago  in Social & Random

Type a product name