Retrieving What A User Has Listened to on Facebook

One of the coolest things to hit Facebook in the past 6 months is the emergence of a user’s music listening habits being added to the news feed. If you haven’t noticed already whenever you or your friends listen to music on say Spotify or Rdio the listening history is sent to Facebook (if enabled). This then shows up in the feed section of your Facebook homepage. The even better news for developers is that these listening habits can now be retrieved via the Graph API.

First, you’ll need to add a few permissions to start retrieving this data. The two extra permissions are user_actions.music and friends_actions.music. This allow you to retrieve the user’s history as well as their friends. Secondly, you’ll have to find the correct endpoint to retrieve the data. Currently, for yourself (the me object):

https://graph.facebook.com/me/music.listens/?access_token=MyAccessToken

And, for anyone else having valid permissions and their Facebook user ID:

https://graph.facebook.com/FacebookUserId/music.listens/?access_token=MyAccessToken

This will give you the list of songs that he user has listened to, however, it does not provide the artist information that many of us desire so we must look elsewhere. Inside the musician object in the response there is an ID, take this and simply call (note it is public, no token needed):

https://graph.facebook.com/ArtistID/

For “Neon Horse” their ID is 10150147572617186 so we get https://graph.facebook.com/10150147572617186/ and the response:

{
   "url": "http://open.spotify.com/artist/3OxN6ElrO3HdkshoGa78N5",
   "type": "profile",
   "title": "Neon Horse",
   "image": [
      {
         "url": "http://o.scdn.co/thumb/9b590e1377742f77bced3d228d8193e6309e5225"
      }
   ],
   "audio": [
      {
         "url": "spotify:artist:3OxN6ElrO3HdkshoGa78N5",
         "type": "audio/vnd.facebook.bridge"
      }
   ],
   "description": "Neon Horse, an artist on Spotify.",
   "site_name": "Spotify",
   "updated_time": "2012-02-14T19:17:57+0000",
   "id": "10150147572617186",
   "application": {
      "id": "174829003346",
      "name": "Spotify",
      "url": "http://www.facebook.com/apps/application.php?id=174829003346"
   }
}

The rest is history as they say! Now get to coding!

Leave a Reply

Your email address will not be published.