[GH-ISSUE #218] Not able to view user's song library or tracks. #112

Closed
opened 2026-02-27 23:20:53 +03:00 by kerem · 5 comments
Owner

Originally created by @JLDevOps on GitHub (Oct 9, 2017).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/218

Hi,

I was looking at the authorization flow for my script and was able to complete the steps.

However, when I run the following example code (at the bottom), the output is my song-library and not another user's library. Is it possible to retrieve a user's library?

import sys
import spotipy
import spotipy.util as util

scope = 'user-library-read'

if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    print "Usage: %s username" % (sys.argv[0],)
    sys.exit()

token = util.prompt_for_user_token(username, scope)

if token:
    sp = spotipy.Spotify(auth=token)
    results = sp.current_user_saved_tracks()
    for item in results['items']:
        track = item['track']
        print track['name'] + ' - ' + track['artists'][0]['name']
else:
    print "Can't get token for", username

Running - python spotify-test.py 12122333515 as a test, would result in my library and not theirs.

Originally created by @JLDevOps on GitHub (Oct 9, 2017). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/218 Hi, I was looking at the authorization flow for my script and was able to complete the steps. However, when I run the following example code (at the bottom), the output is my song-library and not another user's library. Is it possible to retrieve a user's library? ``` import sys import spotipy import spotipy.util as util scope = 'user-library-read' if len(sys.argv) > 1: username = sys.argv[1] else: print "Usage: %s username" % (sys.argv[0],) sys.exit() token = util.prompt_for_user_token(username, scope) if token: sp = spotipy.Spotify(auth=token) results = sp.current_user_saved_tracks() for item in results['items']: track = item['track'] print track['name'] + ' - ' + track['artists'][0]['name'] else: print "Can't get token for", username ``` Running - ```python spotify-test.py 12122333515``` as a test, would result in my library and not theirs.
kerem closed this issue 2026-02-27 23:20:54 +03:00
Author
Owner

@ritiek commented on GitHub (Oct 9, 2017):

I wrote some code to perform this in the past. There you go:

import spotipy
import spotipy.oauth2 as oauth2
import sys

if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    print "Usage: %s username" % (sys.argv[0],)
    sys.exit()


def generate_token():
    credentials = oauth2.SpotifyClientCredentials(
        client_id=client_id,
        client_secret=client_secret)
    token = credentials.get_access_token()
    return token


def get_playlists(username):
    playlists = spotify.user_playlists(username)
    check = 1

    while True:
        for playlist in playlists['items']:
            # in rare cases, playlists may not be found, so playlists['next']
            # is None. Skip these.
            if playlist['name'] is not None:
                print('')
                print('playlist:')
                playlist_title = playlist['name'] + ' - ' + str(playlist['tracks']['total'])
                playlist_title += ' tracks'
                print(playlist_title)
                show_playlist(playlist)
                check += 1
        if playlists['next']:
            playlists = spotify.next(playlists)
        else:
            break


def show_playlist(playlist):
    results = spotify.user_playlist(
        playlist['owner']['id'], playlist['id'], fields='tracks,next')

    tracks = results['tracks']
    show_tracks(tracks)


def show_tracks(tracks):
    n = 1
    while True:
        for item in tracks['items']:
            track = item['track']
            track_title = str(n) + '. '
            track_title += track['name'] + ' - ' + track['artists'][0]['name']
            print(track_title)
            n += 1
        # 1 page = 50 results
        # check if there are more pages
        if tracks['next']:
            tracks = spotify.next(tracks)
        else:
            break

token = generate_token()
spotify = spotipy.Spotify(auth=token)
get_playlists(username)

Usage:

python spotify-test.py username

<!-- gh-comment-id:335065470 --> @ritiek commented on GitHub (Oct 9, 2017): I wrote some code to perform this in the past. There you go: ```python import spotipy import spotipy.oauth2 as oauth2 import sys if len(sys.argv) > 1: username = sys.argv[1] else: print "Usage: %s username" % (sys.argv[0],) sys.exit() def generate_token(): credentials = oauth2.SpotifyClientCredentials( client_id=client_id, client_secret=client_secret) token = credentials.get_access_token() return token def get_playlists(username): playlists = spotify.user_playlists(username) check = 1 while True: for playlist in playlists['items']: # in rare cases, playlists may not be found, so playlists['next'] # is None. Skip these. if playlist['name'] is not None: print('') print('playlist:') playlist_title = playlist['name'] + ' - ' + str(playlist['tracks']['total']) playlist_title += ' tracks' print(playlist_title) show_playlist(playlist) check += 1 if playlists['next']: playlists = spotify.next(playlists) else: break def show_playlist(playlist): results = spotify.user_playlist( playlist['owner']['id'], playlist['id'], fields='tracks,next') tracks = results['tracks'] show_tracks(tracks) def show_tracks(tracks): n = 1 while True: for item in tracks['items']: track = item['track'] track_title = str(n) + '. ' track_title += track['name'] + ' - ' + track['artists'][0]['name'] print(track_title) n += 1 # 1 page = 50 results # check if there are more pages if tracks['next']: tracks = spotify.next(tracks) else: break token = generate_token() spotify = spotipy.Spotify(auth=token) get_playlists(username) ``` Usage: `python spotify-test.py username`
Author
Owner

@JLDevOps commented on GitHub (Oct 9, 2017):

@ritiek Thanks Ritiek for the big help. This works perfectly.

<!-- gh-comment-id:335164666 --> @JLDevOps commented on GitHub (Oct 9, 2017): @ritiek Thanks Ritiek for the big help. This works perfectly.
Author
Owner

@tatoosh commented on GitHub (Feb 9, 2018):

@ritiek ile
do i have to set globals before running ?
"scode.py", line 14, in generate_token
client_id=client_id,
NameError: global name 'client_id' is not defined

<!-- gh-comment-id:364462422 --> @tatoosh commented on GitHub (Feb 9, 2018): @ritiek ile do i have to set globals before running ? "scode.py", line 14, in generate_token client_id=client_id, NameError: global name 'client_id' is not defined
Author
Owner

@ritiek commented on GitHub (Feb 9, 2018):

@tatoosh You need to modify generate_token() to replace it with your actual Client ID and Client Secret you received from Spotify.

client_id='ACTUAL_CLIENT_ID',
client_secret='ACTUAL_CLIENT_SECRET'
<!-- gh-comment-id:364481658 --> @ritiek commented on GitHub (Feb 9, 2018): @tatoosh You need to modify `generate_token()` to replace it with your actual Client ID and Client Secret you received from Spotify. ``` client_id='ACTUAL_CLIENT_ID', client_secret='ACTUAL_CLIENT_SECRET' ```
Author
Owner

@tatoosh commented on GitHub (Feb 9, 2018):

@ritiek ok, thought this could be args vor global vars. Thx

<!-- gh-comment-id:364577755 --> @tatoosh commented on GitHub (Feb 9, 2018): @ritiek ok, thought this could be args vor global vars. Thx
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#112
No description provided.