I want to build an application that can retrieve a user’s listening history from Spotify.
When you look at someone’s Facebook profile with timeline turned on, there’s a Music section that shows Recent Activity. You can see all the tracks they’ve been playing recently.
I’m wondering if there’s any way to get this information programmatically. Does Spotify have an API endpoint that gives access to recently played songs? Or maybe Facebook has something that lets you pull this music data?
I’ve been searching through the documentation but I’m not sure which API calls would work for this. Has anyone successfully implemented something similar? Any guidance on the right approach would be really helpful.
I’ve been working on something similar recently. Spotify’s Web API has a “Get Recently Played Tracks” endpoint that’s perfect for what you need. You’ll need OAuth 2.0 authentication and the “user-read-recently-played” scope to grab the last 50 tracks with playback details. Just remember users need Spotify accounts and have to approve your app’s permissions. Facebook’s API isn’t worth bothering with anymore - they’ve locked down music data pretty hard. Stick with Spotify.
Spotify’s your best option. I built something similar last year and hit a few snags you should know about. The recently played endpoint only goes back 24-48 hours depending on how much someone listens. Plus, if they’re in private mode, those tracks won’t show up at all. Watch out for rate limiting - Spotify’s pretty strict about it. The auth flow’s easy once you figure it out; just store those refresh tokens properly so people don’t have to keep logging back in. Don’t bother with Facebook’s music stuff - they’ve basically killed it off.