Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mager/1792366 to your computer and use it in GitHub Desktop.
Save mager/1792366 to your computer and use it in GitHub Desktop.
Spotify Apps API - Get a user's listening history from Facebook (JS)
/* Instantiate the global sp object; include models & views */
var sp = getSpotifyApi(1);
var auth = sp.require('sp://import/scripts/api/auth');
var permissions = ['user_actions.music'];
var app_id = '126891607432106';
var request_url = 'https://graph.facebook.com/me/music.listens';
auth.authenticateWithFacebook(app_id, permissions, {
onSuccess : function(accessToken, ttl) {
var url = request_url + '?access_token=' + accessToken;
$.getJSON(url, function(data) {
var listens = data.data;
for(var i=0;i<listens.length;i++) {
var tracklink = listens[i].data.song.url;
var trackname = listens[i].data.song.title;
$('#listens').append('<li><a href="' + tracklink + '">' + trackname + '</a></li>');
}
});
},
onFailure : function(error) {
console.log("Authentication failed with error: " + error);
},
onComplete : function() { }
});
@einfalles
Copy link

Thanks for this! Going to play around...Does this always work? What if a user doesn't post their song to Facebook?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment