无法使用Facebook Graph API获取好友列表

时间:2015-02-12 07:53:16

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

这是我第一次使用Facebook Graph API。我需要得到每个朋友的名字谁正在使用我的app.I尝试了很多在线可用的方法,但到目前为止无法设置拉朋友列表。我无法做到这一点。请帮忙。 这是我到目前为止所做的。

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<div id="fb-root"></div>

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxxxxxxxxxxxxxxxxxxx', // Set YOUR APP ID
      channelUrl : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    FB.Event.subscribe('auth.authResponseChange', function(response) 
    {
     if (response.status === 'connected') 
    {
        document.getElementById("message").innerHTML +=  "<br>Connected to Facebook";
        //SUCCESS

    }    
    else if (response.status === 'not_authorized') 
    {
        document.getElementById("message").innerHTML +=  "<br>Failed to Connect";

        //FAILED
    } else 
    {
        document.getElementById("message").innerHTML +=  "<br>Logged Out";

        //UNKNOWN ERROR
    }
    }); 

    };

    function Login()
    {

        FB.login(function(response) {
           if (response.authResponse) 
           {
                getUserInfo();
            } else 
            {
             console.log('User cancelled login or did not fully authorize.');
            }
         },{scope: 'email,user_photos,user_videos'});

    }


  function getUserInfo() {
        FB.api('/me', function(response) {

      var str="<b>Name</b> : "+response.name+"<br>";
          str +="<b>Link: </b>"+response.link+"<br>";
          str +="<b>Username:</b> "+response.username+"<br>";
          str +="<b>id: </b>"+response.id+"<br>";
          str +="<b>Email:</b> "+response.email+"<br>";
          str +="<input type='button' value='Get Photo' onclick='getPhoto();'/>";
          str +="<input type='button' value='Logout' onclick='Logout();'/>";
          document.getElementById("status").innerHTML=str;

    });
    }

function getFriends() {
    FB.api('/me/friends', function(response) {
        if(response.data) {
            $.each(response.data,function(index,friend) {
                alert(friend.name + ' has id:' + friend.id);
            });
        } else {
            alert("Error!");
        }
    });
}
    function getPhoto()
    {
      FB.api('/me/picture?type=normal', function(response) {

          var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
          document.getElementById("status").innerHTML+=str;

    });

    }

    function Logout()
    {
        FB.logout(function(){document.location.reload();});
    }

  // Load the SDK asynchronously
  (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>
<div align="center">
<h2>Facebook OAuth Javascript Demo</h2>

<div id="status">
 Click on Below Image to start the demo: <br/>
<img src="http://hayageek.com/examples/oauth/facebook/oauth-javascript/LoginWithFacebook.png" style="cursor:pointer;" onclick="Login()"/>
</div>

<br/><br/><br/><br/><br/>

<div id="message">
Logs:<br/>
</div>

</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

如果朋友们也授权你的应用程序,那么他们只会出现,这一点非常重要。但我的猜测是你忘了在登录时要求user_friends权限。我没有在范围内看到它。

不过,您还应该使用JS SDK的最新代码:https://developers.facebook.com/docs/javascript/quickstart/v2.2

答案 1 :(得分:0)

将此代码替换为以前的代码,并从代码中删除旧代码并匹配}

  window.fbAsyncInit = function() {
FB.init({
      appId      : 'XXXXXXXXXXXXX',
      xfbml      : true,
      version    : 'v2.2'
    });
  };
  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));