You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spotify API获取最近播放记录时next链接返回空数组问题咨询

Fixing Empty tracks Array in Spotify's recently-played Pagination

I’ve run into this exact issue before with Spotify’s API—let’s walk through the most likely reasons you’re getting an empty tracks array when using the before URL from the next property, even though you know you’ve listened to way more than 50 tracks:

1. Timestamp Edge Case

Spotify uses millisecond-precision timestamps for the before parameter, but sometimes the auto-generated next URL’s timestamp lands exactly at a point where there are no tracks immediately before it. A quick fix is to manually adjust the timestamp by subtracting a few seconds (e.g., 10,000 milliseconds) to capture the tracks just before that cutoff.

Instead of using the exact before value from the next URL:

https://api.spotify.com/v1/me/player/recently-played?before=1557334757252&limit=50

Try tweaking it slightly:

https://api.spotify.com/v1/me/player/recently-played?before=1557324757252&limit=50

2. Spotify's Data Retention Window

Keep in mind that Spotify only stores your recently played tracks for a limited time—usually around 4 weeks (this isn’t officially documented, but it’s a common observation). If the tracks you’re trying to fetch are older than this window, the API will return an empty array regardless of how many you played earlier.

3. Missing Authorization Scope

Double-check that your access token includes the user-read-recently-played scope. If you authenticated your app without this scope initially, you might get partial results on the first request and empty arrays when paginating. Verify your token’s scopes by calling https://api.spotify.com/v1/me and checking the scope field in the response.

4. Pagination Logic Workaround

Instead of relying solely on the auto-generated next URL, try building your own pagination request using the played_at timestamp of the oldest track from your initial 50 results. Use that timestamp as the before parameter—this ensures you’re fetching tracks played right before the last one in your current dataset, which is more reliable than the auto-generated URL in some cases.

5. Temporary API Glitch

Occasionally, Spotify’s API has blips with pagination. Try re-sending the request after a few minutes, or test with a fresh access token to rule out any token-specific issues.


内容的提问来源于stack exchange,提问作者Alex T

火山引擎 最新活动