admin管理员组文章数量:1244314
I want to display photos from one of my facebook albums on my webpage with the javascript api. I can read the album names with the below code, but how can I then display the photos of one of the albums?
FB.api('/myfacebookid/albums', function(response) {
var ul = document.getElementById('albums');
for (var i=0, l=response.data.length; i<l; i++) {
var
album = response.data[i],
li = document.createElement('li'),
a = document.createElement('a');
a.innerHTML = album.name;
a.href = album.link;
li.appendChild(a);
ul.appendChild(li);
}
});
Any input appreciated, thanks!
OK, now I have this code instead:
FB.api('/142461229141170/albums?fields=id,name', function(response) {
if (response && response.data && response.data.length){
console.log(response)
for (var i=0; i<response.data.length; i++) {
var album = response.data[i];
if (album.name == 'Profile Pictures'){
FB.api('/'+album.id+'/photos', function(photos){
if (photos && photos.data && photos.data.length){
for (var j=0; j<photos.data.length; j++){
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.src = photo.picture;
$('#foton').append(image);
}
}
});
break;
}
}
}
});
I guess Im doing something wrong..still! This is displaying the photos from the Profile Pictures album but I had to change it to $('#foton').append(image); . If I look at the log it gets 4 objects.
If I change to a different album eg - Manmade Photos or Cover Photos it stops working.
And also if I change the userId from above(the above is a PAGE id) to my personal facebook id then it stops working aswell.
Im really trying to understand how this works, because I also want to get the feed and some other stuff after this, and I guess that it works the same way as this? Thanks a lot, guys!
I want to display photos from one of my facebook albums on my webpage with the javascript api. I can read the album names with the below code, but how can I then display the photos of one of the albums?
FB.api('/myfacebookid/albums', function(response) {
var ul = document.getElementById('albums');
for (var i=0, l=response.data.length; i<l; i++) {
var
album = response.data[i],
li = document.createElement('li'),
a = document.createElement('a');
a.innerHTML = album.name;
a.href = album.link;
li.appendChild(a);
ul.appendChild(li);
}
});
Any input appreciated, thanks!
OK, now I have this code instead:
FB.api('/142461229141170/albums?fields=id,name', function(response) {
if (response && response.data && response.data.length){
console.log(response)
for (var i=0; i<response.data.length; i++) {
var album = response.data[i];
if (album.name == 'Profile Pictures'){
FB.api('/'+album.id+'/photos', function(photos){
if (photos && photos.data && photos.data.length){
for (var j=0; j<photos.data.length; j++){
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.src = photo.picture;
$('#foton').append(image);
}
}
});
break;
}
}
}
});
I guess Im doing something wrong..still! This is displaying the photos from the Profile Pictures album but I had to change it to $('#foton').append(image); . If I look at the log it gets 4 objects.
If I change to a different album eg - Manmade Photos or Cover Photos it stops working.
And also if I change the userId from above(the above is a PAGE id) to my personal facebook id then it stops working aswell.
Im really trying to understand how this works, because I also want to get the feed and some other stuff after this, and I guess that it works the same way as this? Thanks a lot, guys!
Share Improve this question edited Mar 7, 2012 at 18:34 Claes Gustavsson asked Mar 7, 2012 at 13:06 Claes GustavssonClaes Gustavsson 5,67911 gold badges55 silver badges88 bronze badges2 Answers
Reset to default 8This can be done by issuing Graph API request to photos
connection of album
:
FB.api('/me/albums?fields=id,name', function(response) {
for (var i=0; i<response.data.length; i++) {
var album = response.data[i];
if (album.name == 'Profile Pictures'){
FB.api('/'+album.id+'/photos', function(photos){
if (photos && photos.data && photos.data.length){
for (var j=0; j<photos.data.length; j++){
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.src = photo.picture;
document.body.appendChild(image);
}
}
});
break;
}
}
});
You use the graph to do it... all you need is the album.id
from above, and then you make a call to the graph to get the photos. I passed fields=pictures
to tell facebook to only return the picture links. You can omit this parameter and it will bring you a whole bunch of stuff back.
https://graph.facebook./<album.id>/photos?fields=picture
The list of all of the fields can be found here Album - Graph API
本文标签: display photos from facebook with javascript apiStack Overflow
版权声明:本文标题:display photos from facebook with javascript api? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740210935a2241984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论