I'm using the Spotify API with Python (using the client credentials flow) to fetch audio features for tracks. My code successfully retrieves an access token and can search for an artist, but when I try to get the audio features for certain tracks, I receive a 403 Forbidden error.
Below is a simplified version of my code:
from dotenv import load_dotenv
import requests
import os
import base64
import json
load_dotenv()
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
def get_token():
auth_string = f"{client_id}:{client_secret}"
auth_bytes = auth_string.encode('utf-8')
auth_base64 = str(base64.b64encode(auth_bytes), 'utf-8')
url = ";
headers = {
'Authorization': f"Basic {auth_base64}",
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {'grant_type': 'client_credentials'}
result = requests.post(url, headers=headers, data=data)
json_result = json.loads(result.text)
if 'access_token' in json_result:
token = json_result['access_token']
return token
else:
raise Exception("Failed to retrieve access token")
def get_auth_header(token):
return {'Authorization': f"Bearer {token}"}
def search_spotify(query, types, token, market=None, limit=20, offset=0, include_external=None):
url = ";
headers = get_auth_header(token)
params = {
'q': query,
'type': ','.join(types),
'limit': limit,
'offset': offset
}
if market:
params['market'] = market
if include_external:
params['include_external'] = include_external
result = requests.get(url, headers=headers, params=params)
return result.json()
def get_song_features(song_id, token):
url = f"/{song_id}"
headers = get_auth_header(token)
result = requests.get(url, headers=headers)
json_result = json.loads(result.content)
# Imprime la respuesta completa para depuración
print(json_result)
return json_result
token = get_token()
query = "ACDC"
types = ["track", "artist"]
result = search_spotify(query, types, token, market="AR", limit=10)
for track in result['tracks']['items']:
song_name = track['name']
song_id = track['id']
features = get_song_features(song_id, token)
if 'error' in features:
print(f"Error fetching features for song: {song_name}")
else:
print(f"Song: {song_name}")
print(f"Features: {features}")
print()
Here is the response:
{'error': {'status': 403}}
Error fetching features for song: Thunderstruck
{'error': {'status': 403}}
Error fetching features for song: Highway to Hell
{'error': {'status': 403}}
Error fetching features for song: Back In Black
I tried multiple artists (just in case) and the error is the same. I also tried using spotipy and I couldn't either.
Is this behavior expected for certain remastered tracks due to licensing or regional restrictions?
Has anyone encountered similar issues with the Spotify API returning 403 errors when fetching audio features for specific tracks?
Any help or insights would be greatly appreciated
I'm using the Spotify API with Python (using the client credentials flow) to fetch audio features for tracks. My code successfully retrieves an access token and can search for an artist, but when I try to get the audio features for certain tracks, I receive a 403 Forbidden error.
Below is a simplified version of my code:
from dotenv import load_dotenv
import requests
import os
import base64
import json
load_dotenv()
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
def get_token():
auth_string = f"{client_id}:{client_secret}"
auth_bytes = auth_string.encode('utf-8')
auth_base64 = str(base64.b64encode(auth_bytes), 'utf-8')
url = "https://accounts.spotify.com/api/token"
headers = {
'Authorization': f"Basic {auth_base64}",
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {'grant_type': 'client_credentials'}
result = requests.post(url, headers=headers, data=data)
json_result = json.loads(result.text)
if 'access_token' in json_result:
token = json_result['access_token']
return token
else:
raise Exception("Failed to retrieve access token")
def get_auth_header(token):
return {'Authorization': f"Bearer {token}"}
def search_spotify(query, types, token, market=None, limit=20, offset=0, include_external=None):
url = "https://api.spotify.com/v1/search"
headers = get_auth_header(token)
params = {
'q': query,
'type': ','.join(types),
'limit': limit,
'offset': offset
}
if market:
params['market'] = market
if include_external:
params['include_external'] = include_external
result = requests.get(url, headers=headers, params=params)
return result.json()
def get_song_features(song_id, token):
url = f"https://api.spotify.com/v1/audio-features/{song_id}"
headers = get_auth_header(token)
result = requests.get(url, headers=headers)
json_result = json.loads(result.content)
# Imprime la respuesta completa para depuración
print(json_result)
return json_result
token = get_token()
query = "ACDC"
types = ["track", "artist"]
result = search_spotify(query, types, token, market="AR", limit=10)
for track in result['tracks']['items']:
song_name = track['name']
song_id = track['id']
features = get_song_features(song_id, token)
if 'error' in features:
print(f"Error fetching features for song: {song_name}")
else:
print(f"Song: {song_name}")
print(f"Features: {features}")
print()
Here is the response:
{'error': {'status': 403}}
Error fetching features for song: Thunderstruck
{'error': {'status': 403}}
Error fetching features for song: Highway to Hell
{'error': {'status': 403}}
Error fetching features for song: Back In Black
I tried multiple artists (just in case) and the error is the same. I also tried using spotipy and I couldn't either.
Is this behavior expected for certain remastered tracks due to licensing or regional restrictions?
Has anyone encountered similar issues with the Spotify API returning 403 errors when fetching audio features for specific tracks?
Any help or insights would be greatly appreciated
This is probably because the API was deprecated on November 27, 2024. However, I'm not entirely sure why that would cause a 403 error though.