使用 javascript api 显示来自 facebook 的照片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9602086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:11:00  来源:igfitidea点击:

display photos from facebook with javascript api?

javascriptfacebookfacebook-graph-apifacebook-javascript-sdk

提问by Claes Gustavsson

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?

我想使用 javascript api 在我的网页上显示我的 facebook 相册之一中的照片。我可以使用以下代码读取相册名称,但是如何显示其中一个相册的照片?

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.

我想我做错了什么..仍然!这是显示 Profile Pictures 相册中的照片,但我不得不将其更改为 $('#foton').append(image); . 如果我查看日志,它会得到 4 个对象。

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.

而且,如果我将上面的 userId(上面是 PAGE id)更改为我的个人 facebook id,那么它也会停止工作。

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!

我真的很想了解这是如何工作的,因为我还想在此之后获得提要和其他一些东西,我猜它的工作方式与此相同?非常感谢,伙计们!

回答by Juicy Scripter

This can be done by issuing Graph API request to photosconnection of album:

这可以通过向以下photos连接发出 Graph API 请求来完成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;
    }
  }
});

回答by Nix

You use the graph to do it... all you need is the album.idfrom above, and then you make a call to the graph to get the photos. I passed fields=picturesto tell facebook to only return the picture links. You can omit this parameter and it will bring you a whole bunch of stuff back.

你用图表来做……你需要的只是album.id上面的,然后你调用图表来获取照片。我通过fields=pictures告诉 facebook 只返回图片链接。你可以省略这个参数,它会给你带来一大堆东西。

https://graph.facebook.com/<album.id>/photos?fields=picture

The list of all of the fields can be found here Album - Graph API

可以在此处找到所有字段的列表Album - Graph API