Is that real to pull only one (first or last, no matter) photo from each album in the Facebook JavaScript SDK?
I’m tried to make this with a cycle for each album, but I think it is not good idea to do requests to Facebook for every album, especially if user has 50 or more albums. I need it to display the thumbnail next to each album.
What I tried:
FB.api('/me/albums?access_token='+response.authResponse.accessToken,function(response){
if(response.error){
alert(response.error);
}
else {
var albums = new Array();
albums = response.data;
var albumsCount = albums.length;
var albumsString = "";
var i = 0;
for(i = 0; i<albumsCount;i++){
//Here is that bad idea with requests in every cycle
FB.api('/'+albums[i].id+'/photos?access_token='+FB.getAuthResponse().accessToken,function(response){
if(response.error) {
alert(response.error);
}
else {
var photo=response.data;
var photoHere="<img src='"+photo[i].picture+"'>";
}
});
//End of bad idea
albumsString+="<a href='#' return false'>"+albums[i].name+"</a><br/>";
}
var toHTML="List of your albums: "+albumString+"<br/>";
document.getElementById("resultAlbums").innerHTML=toHTML;
}
});
It seems like you’re looking for the cover photo of each album. One trick I found was to use the /picture connection from an album id.
So, https://graph.facebook.com/%5Balbum_id%5D/picture will redirect to the cover image for that album.
That’ll remove the need for the inner, slow loop.
You’ll need to authenticate by adding ?access_token=.. to the url to show covers of non-public albums.