0

我正在使用 facebook 登录及其图形 api 将用户的所有图像列出到我的网站,以下代码仅适用于我是 facebook 应用程序的管理员和所有者,当他登录时它不适用于任何其他人该网站使用 facebook 登录。

代码说明:当用户登录时,调用一个名为testAPI的函数,获取用户基本信息,然后再次调用FB.API获取权限,最后获取图片。

权限参数“res”对任何其他用户都没有任何作用,但它对我(管理员)有用。
继承人的代码:

<div id="fb-root"></div>
 <script>
  // Additional JS functions here
  window.fbAsyncInit = function() {
  FB.init({
   appId      : MY_APP_ID, // App ID
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
   });


   FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
                testAPI();
 } else if (response.status === 'not_authorized') {
       login();
     } else {
       login();
      }
     });

   };


    function login() {
       FB.login(function(response) {
           if (response.authResponse) {
            testAPI();
            } else {
           // cancelled
           }
         },{scope:'user_photos',perms:'user_photos'});
  }
   var id;
   function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
    console.log(response);
    id=response.id;

var link='/'+ id + '/permissions?access_token=FB_GENERATED_ACCESS_TOKEN';
 FB.api(link,function(res){
 console.log("permissons: ",res);
     link='/'+ id + '/photos?fields=images';
     FB.api(link, function(response) {
      //placing all pictures
      for(var i=0;i<response.data.length;i++)
      {
            var img="<img src="+response.data[i].images[0].source+" class='small' />";
         $("#images").append(img);
    }      
      console.log(response);
       });      

 });

    });}
   (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));
 </script></body>

  <fb:login-button autologoutlink='true'   
  perms='email,user_birthday,status_update,publish_stream'></fb:login-button>
 <div id="images"></div>
4

2 回答 2

3

我得到了你的实际问题,

<fb:login-button autologoutlink='true' 
 perms='email,user_birthday,status_update,publish_stream'></fb:login-button>

您在这里错过了 user_photos 权限。你的 javascript 函数没有调用,因为 fb:login-button 自己做所有的事情。你的新代码应该是:

<fb:login-button autologoutlink='true' 
     perms='email,user_birthday,status_update,publish_stream,user_photos'></fb:login-button>
于 2013-02-24T05:21:19.257 回答
0

代替

var link='/'+ id + '/permissions?access_token=FB_GENERATED_ACCESS_TOKEN';
FB.api(link,function(res){
console.log("permissons: ",res);
 link='/'+ id + '/photos?fields=images';
 FB.api(link, function(response) {
  //placing all pictures
  for(var i=0;i<response.data.length;i++)
  {
        var img="<img src="+response.data[i].images[0].source+" class='small' />";
     $("#images").append(img);
}      
  console.log(response);
   });      

试试这个:

FB.api('/me/permissions', function (response) {
    console.log("permissons: ", res);
    var perms = response.data[0];

    if (perms.user_photos) {

        FB.api('/me/photos?fields=images', function (response) {
            //placing all pictures
            for (var i = 0; i < response.data.length; i++) {
                var img = "<img src=" + response.data[i].images[0].source + " class='small' />";
                $("#images").append(img);
            }
            console.log(response);
        } else {
            // User DOESN'T have permission. Perhaps ask for them again with FB.login?
            login();
        }
        });
于 2013-02-20T13:53:30.180 回答