Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't access tracks audio features using Spotipy fetching from Spotify API

Doing a project in university data science course and I’m working with API-s for the first time. I need to fetch different data about tracks using Spotify API, but I have encountered a problem early on. I can get access to some basic data about tracks popularity, duration, etc but I get 403 error when trying to fetch audio-features aswell. As far as I have searched for help on the web, apparently I have insufficient permissions in the scope of my Spotify access token. But I don’t know how to get this problem fixed.

I attempted to fetch audio features (e.g., tempo, danceability, energy) for the same tracks using sp.audio_features(track_ids). This is where the issue arises.
When I run the code, I receive an error, and the audio features are not retrieved.

Error: "HTTP Error for GET to https://api.spotify.com… with Params: {} returned 403 due to None"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from spotipy.oauth2 import SpotifyOAuth
import spotipy
from dotenv import load_dotenv
import os
import pandas as pd
    
load_dotenv()
client_id = os.getenv("SPOTIPY_CLIENT_ID")
client_secret = os.getenv("SPOTIPY_CLIENT_SECRET")
redirect_uri = os.getenv("SPOTIPY_REDIRECT_URI")

auth_manager = SpotifyOAuth(
    client_id=client_id,
    client_secret=client_secret,
    redirect_uri=redirect_uri,
    scope="user-top-read"
)
sp = spotipy.Spotify(auth_manager=auth_manager)
    
# Fetch data
top_tracks = sp.current_user_top_tracks(limit=10)
track_data = []
track_ids = []
for item in top_tracks['items']:
    track_ids.append(item['id'])
    track_data.append({
        'track_id': item['id'],
        'track_name': item['name'],
        'artists': ", ".join(artist['name'] for artist in item['artists']),
        'popularity': item['popularity'],
        'explicit': item['explicit'],
        'duration_ms': item['duration_ms']
    })

# Fetch audio features
try:
    audio_features = sp.audio_features(track_ids) # Causing errors
    audio_features_df = pd.DataFrame(audio_features)
    df = pd.DataFrame(track_data)
    df = pd.merge(df, audio_features_df, left_on='track_id', right_on='id')
except: 
    print("Can not access audio features")
    df = pd.DataFrame(track_data)
    
df.head(10)

>Solution :

Spotify deprecated several API endpoints on November 27th 2024. The get-audio-features was one of those endpoints.

This in turn affected spotipy as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading