Javascript 如何使用 facebook API 获取相册图片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5276088/
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-08-23 16:26:47  来源:igfitidea点击:

How do I get album pictures using facebook API?

javascriptfacebook

提问by okysabeni

This should be fairly common but somehow I cannot get it to work. What I would like to do is the get album pictures from facebook. I am implementing this on a website.

这应该是相当普遍的,但不知何故我无法让它工作。我想做的是从 facebook 获取专辑图片。我正在一个网站上实施这个。

I can get the albums using this code:

我可以使用此代码获取专辑:

function getAlbumPhotos(){
            FB.api('/me/albums',  function(resp) {
                //Log.info('Albums', resp);
                var ul = document.getElementById('albums');
                for (var i=0, l=resp.data.length; i<l; i++){
                    var
                        album = resp.data[i],
                        li = document.createElement('li'),
                        a = document.createElement('a');
                    a.innerHTML = album.name;
                    a.href = album.link;
                    li.appendChild(a);
                    ul.appendChild(li);
                }
            });
        };

the resp returns a data array which contains links to the photo albums BUT I would like the image sources for each album and I don't see anything I can use in the resp data. The data object contains a link to the album but not individual images.

resp 返回一个数据数组,其中包含指向相册的链接,但我想要每个相册的图像源,但我在 resp 数据中看不到任何可以使用的内容。数据对象包含指向相册的链接,但不包含单个图像。

According to facebook documentation, photos are "connections" to albums. I am not sure what means but their doc shows that you can get individual photos.

根据 facebook 文档,照片是与相册的“连接”。我不确定是什么意思,但他们的文档显示您可以获得单张照片。

From this link:

从这个链接:

[http://developers.facebook.com/docs/reference/api/album/][1]

it shows the json(?) returns link, id, name, etc...which I am able to get. However, at the bottom of that page are "connections" to album which includes photos, comments, pictures. When I click on photos, it shows the JSON data structure including the img src. Question is, how do I get that? It seems so straightforward but I can't get it to work.

它显示了 json(?) 返回链接、id、名称等......我能够得到。但是,在该页面的底部是与相册的“连接”,其中包括照片、评论、图片。当我点击照片时,它会显示包含 img src 的 JSON 数据结构。问题是,我怎么得到它?这看起来很简单,但我无法让它工作。

I tried

我试过

FB.api('/me/photos',function(resp) ...

and

FB.api('/me/photo',function(resp) ...

photos return nothing while photo returns undefine.

照片不返回任何内容,而照片返回未定义。

Code samples will be greatly appreciated.

代码示例将不胜感激。

回答by ifaour

  1. From the first call you get all the albums (and the album IDs) '/me/albums'
  2. from there you can get the album picture (cover) '/'+album.id+'/picture'
  3. AND the photos of the album '/'+album.id+'/photos'
  1. 从第一次通话开始,您将获得所有专辑(和专辑 ID) '/me/albums'
  2. 从那里你可以得到专辑图片(封面) '/'+album.id+'/picture'
  3. 和相册的照片 '/'+album.id+'/photos'

回答by Karl Kastan

you can also try

你也可以试试

/_ABLUM_ID_/photos

/_ABLUM_ID_/照片

on the graph api, i.e.

在图形api上,即

https://graph.facebook.com/12341234/photos

https://graph.facebook.com/12341234/photos

where 12341234 is the album object id of the album you want to get the photos of.

其中 12341234 是您要获取照片的相册的相册对象 ID。

回答by chaitsi

FB.api("/"+albumid+"/photos",function(response){
    var photos = response["data"];
    document.getElementById("photos_header").innerHTML = "Photos("+photos.length+")";
    for(var v=0;v<photos.length;v++) {
        var image_arr = photos[v]["images"];

        var subImages_text1 = "Photo "+(v+1);

        //this is for the small picture that comes in the second column
        var subImages_text2 = '<img src="'+image_arr[(image_arr.length-1)]["source"]+'" />';

        //this is for the third column, which holds the links other size versions of a picture
        var subImages_text3 = "";

        //gets all the different sizes available for a given image
        for(var j = 0 ;j<image_arr.length;j++) {
            subImages_text3 += '<a target="_blank" href="'+image_arr[j]["source"]+'">Photo('+image_arr[j]["width"]+"X"+image_arr[j]["height"]+')</a><br/>';
        }
        addNewRow(subImages_text1,subImages_text2,subImages_text3);
    }
});

回答by bini

        <script src="http://connect.facebook.net/en_US/all.js"></script>

        <script type="text/javascript">
            var loggedIn = false;        

            function loginFacebook() 
            {
                //initializes the facebook API
            }        




            function loadAlbums()
            {            
                 FB.init({
            appId  : '345203265493024',
            status : true, // check login status
            cookie : true, // enable cookies to allow the server to access the session
            xfbml  : true  // parse XFBML
              });





FB.login(function(response)
{
if (response.authResponse)
 {

//Logged in and accepted permissions!

       document.getElementById("status").innerHTML = "Getting album information from your Facebook profile";
                var counter = 0;

      // Start Normal API
                FB.api('/me/albums', function(response) 
                {


                  var d=response.data;



                  for (var i=0, l=d.length; i<l; i++)
                    {
                        addOption(response["data"][i].name,response["data"][i].id);
                        counter++;


                    }
                    document.getElementById("status").innerHTML = "There are "+ counter +" albums in your Facebook profile";
                });


                 //end of  Normal API

        document.getElementById("albumBtn").style.visibility="hidden";   



}
},{scope:'read_stream,publish_stream,offline_access,user_photos,friends_photos,user_photo_video_tags,friends_photo_video_tags'});


            }

            //Adds a new option into the drop down box
            function addOption(opText,opVal) 
            {
                var v = document.getElementById("albumsList");             
                v.innerHTML += '<br/><a  href="facebookphotos.aspx?album='+opVal+'&name='+opText+'">'+opText+'</a>';               
            }

            function init() 
            {
                var v1 = document.getElementById("albumBtn");
                v1.onclick = loadAlbums;
                // v1.style.visibility= "hidden";             
            }

            //calls init function once all the resources are loaded
            window.addEventListener("load",init,true);
        </script>      


this code works

回答by Nino Paolo

You can use this api:

你可以使用这个api:

$photos = $facebook->api('/me?fields=albums.limit(50).fields(photos.limit(50).fields(id,source))');

$photos = $facebook->api('/me?fields=albums.limit(50).fields(photos.limit(50).fields(id,source))');