如何通过Spotify API的albums/{id}/tracks端点获取50首以上曲目信息?
albums/{id}/tracks Endpoint No need to "bypass" anything—Spotify's API is built to handle this exact scenario with pagination parameters: limit and offset. Here's how to use them to grab tracks 51 through 120 using curl and jq:
Key Parameters Explained
limit: The maximum number of tracks returned per request (capped at 50, as you noted).offset: The number of tracks to skip before starting the response. For example,offset=50skips the first 50 tracks, so you’ll get tracks 51-100.
Step-by-Step Requests
Get Tracks 51-100
Useoffset=50and keeplimit=50(since this range fits exactly 50 tracks):curl -X GET "https://api.spotify.com/v1/albums/{YOUR_ALBUM_ID}/tracks?limit=50&offset=50" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" | \ jq '.items[] | {track_name: .name, spotify_id: .id}'Get Tracks 101-120
We only need 20 tracks here, so setlimit=20andoffset=100(skipping the first 100 tracks):curl -X GET "https://api.spotify.com/v1/albums/{YOUR_ALBUM_ID}/tracks?limit=20&offset=100" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" | \ jq '.items[] | {track_name: .name, spotify_id: .id}'
Bonus: Automate Pagination for All Tracks Beyond 50
If you want to fetch all tracks after the first 50 (including 51-120) without manual requests, use a bash loop. First, grab the total number of tracks in the album, then iterate through offsets:
# Get total number of tracks in the album TOTAL_TRACKS=$(curl -s -X GET "https://api.spotify.com/v1/albums/{YOUR_ALBUM_ID}/tracks?limit=1" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" | jq '.total') # Start at offset 50 and loop until we've covered all tracks OFFSET=50 while [ $OFFSET -lt $TOTAL_TRACKS ]; do # Calculate how many tracks to fetch in this request (max 50) LIMIT=$(( TOTAL_TRACKS - OFFSET > 50 ? 50 : TOTAL_TRACKS - OFFSET )) # Fetch and parse the tracks curl -s -X GET "https://api.spotify.com/v1/albums/{YOUR_ALBUM_ID}/tracks?limit=$LIMIT&offset=$OFFSET" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" | \ jq '.items[] | {track_name: .name, spotify_id: .id}' # Move to the next offset OFFSET=$(( OFFSET + 50 )) done
Just replace {YOUR_ALBUM_ID} and YOUR_ACCESS_TOKEN with your actual values, and this will output all tracks from 51 onwards, including the 101-120 range.
内容的提问来源于stack exchange,提问作者Hubert Léveillé Gauvin

