Spotify API Access Token Auto-Renewal Issue in Python Data Extraction Script

Issue Description

I built a Python script that processes music data from Spotify playlists and exports everything to CSV format. The script pulls information about songs, musicians, and albums from multiple playlists using the Spotify Web API.

The Problem

My script runs fine for roughly one hour, then freezes without showing any error messages. It just stops processing tracks and hangs there. I think this happens because the authentication token expires.

I tried adding token refresh logic at the start of my script, but now it gets stuck on the very first song instead of running for an hour like before. The console output just shows “Processing track” and then nothing else happens.

What I Tried

I added code to refresh the access token before the main loop starts. I also switched from user authentication to client credentials flow, thinking it might be more stable. Neither approach worked.

Current Code Structure

import csv
from datetime import datetime, timedelta
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import time

# Authentication setup
client_id = 'your_client_id'
client_secret = 'your_client_secret'

auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
spotify_client = spotipy.Spotify(auth_manager=auth_manager)

# List of playlist IDs to process
playlist_list = ['37i9dQZF1DX4JAvHpjipBk', '37i9dQZF1DX0XUsuxWHRQd', '37i9dQZF1DXdwmD5Q7Gxah']

music_data = []

def renew_token():
    spotify_client.auth_manager.get_access_token(as_dict=False, check_cache=False)

renew_token()

# Main processing loop
for playlist_id in playlist_list:
    playlist_info = spotify_client.playlist(playlist_id)
    track_results = spotify_client.playlist_tracks(playlist_id)
    
    position = 1
    
    for item in track_results['items']:
        current_track = item['track']
        print(f"Processing: {current_track['artists'][0]['name']} - {current_track['name']}")
        
        try:
            artist_info = spotify_client.artist(current_track['artists'][0]['id'])
            album_info = spotify_client.album(current_track['album']['id'])
            
            music_data.append({
                'musician': current_track['artists'][0]['name'],
                'title': current_track['name'],
                'release_date': current_track['album']['release_date'],
                'playlist_position': position,
                'artist_followers': artist_info['followers']['total'],
                'cover_image': current_track['album']['images'][0]['url'],
                'song_popularity': current_track['popularity'],
                'musician_popularity': artist_info['popularity'],
                'record_label': album_info['label'],
                'playlist_id': playlist_id,
                'playlist_title': playlist_info['name']
            })
            position += 1
            
        except Exception as error:
            print(f"Error processing track: {error}")
            renew_token()
            
        time.sleep(1)

Questions

  1. Why does my script freeze without throwing any error messages?
  2. How can I implement proper automatic token refresh during the data extraction process?
  3. Should I be using a different authentication method for long-running scripts?

Any help would be appreciated. Thanks!

Been through this exact Spotify nightmare before. You’re juggling too much stuff manually - that’s your problem.

The hanging comes from hitting rate limits or timeouts during all those sequential API calls. Most don’t throw proper exceptions, so your script just sits there. Add token refresh tracking on top and it gets messy fast.

Ditch the Python token management entirely. Automate the whole thing - API calls, token refresh, data processing, CSV export. Run it continuously or scheduled without babysitting hanging scripts.

Fresh authentication every execution means no more token timing issues. Built-in error handling and retries without writing custom timeout code.

I dumped similar Python scripts for automated workflows on all my data extraction. Way more reliable than debugging silent failures and token refresh logic.

Latenode works great for this - native Spotify integration handles all the auth headaches automatically: https://latenode.com

This looks like a pagination issue - Spotify caps requests at 100 tracks by default. Your script hits that wall and stops because there’s no more data coming through. Check if track_results has a ‘next’ field and loop through all the pages. Also, ditch that manual renew_token() call at the start - you’re actually making things worse. The auth manager handles token expiration on its own when you don’t mess with it.

The freezing happens because spotipy’s token handling gets stuck - not because tokens expire. I hit this same issue pulling large datasets. The library fails silently on network timeouts or rate limits without throwing proper errors. Don’t manually call renew_token(). Let spotipy handle auth automatically. Remove your manual refresh calls and add timeout handling to requests. Client credentials should auto-refresh tokens on its own. You’re probably hitting Spotify’s rate limits - even with time.sleep(1), you’re making multiple API calls per track for artist, album, and track data. Wrap your API calls in a retry decorator with exponential backoff. Batch requests when you can. For the hanging issue, add a timeout parameter when you initialize the spotipy client. This forces failed requests to throw exceptions instead of hanging forever. The auth isn’t your real problem here.

Had the same issue scraping big playlists last year. Your token refresh isn’t being called early enough. Don’t wait for exceptions - check token expiration before making API calls. I fixed it by tracking when I last refreshed and calling renewal every 45 minutes, way before the usual 60-minute timeout. You’re only refreshing after errors happen, but the request already failed by then. Add a timestamp check before your main API calls. Store when you refreshed the token and compare it to current time before processing each playlist. If it’s been over 50 minutes, refresh early. This stopped the hanging I was getting. Also - client credentials flow works fine here since you’re just accessing public playlist data. When it hangs without error messages, the API request is timing out instead of failing properly.