You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spotify API获取音轨音频特征时遭遇403 Forbidden错误的求助

Spotify API获取音轨音频特征时遭遇403 Forbidden错误的求助

我正在使用Python结合Spotify API(客户端凭证流)来获取音轨的音频特征。我的代码可以成功获取访问令牌,也能搜索艺术家,但当尝试获取某些音轨的音频特征时,会收到403 Forbidden错误。

下面是简化版的代码:

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()

返回的响应如下:

{'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

我试过多个艺术家(以防是个别情况),但错误依然存在。我也尝试过用spotipy库,结果还是一样。

这种情况是因为某些重制版音轨的授权或区域限制导致的吗?

有没有人遇到过类似的Spotify API获取特定音轨音频特征时返回403错误的问题?

非常感谢任何帮助或见解!

备注:内容来源于stack exchange,提问作者Lautaro Flores

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 08:35:29