如何使用Javascript + FQL获取朋友的Facebook状态?

时间:2013-07-03 01:46:26

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

我正在尝试用Javascript构建一个Facebook应用程序,让我通过使用FQL检索每个朋友的状态列表。但似乎没有从每个FQL查询返回任何结果。这是我的代码,从各种教程模板拼凑而成:

<html>
<title>Testing Facebook Javascript SDK</title>
<body>
<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        // init the FB JS SDK
        FB.init({
            appId      : 'MY_APP_ID', // App ID from the App Dashboard
        channelUrl: '//www.myurl.net/channel.html',
            status     : true, // check the login status upon init?
            cookie     : true, // set sessions cookies to allow your server to access the session?
            xfbml      : true  // parse XFBML tags on this page?
        });

    // Additional initialization code such as adding Event Listeners goes here
    FB.getLoginStatus(function(response){
        if (response.status === 'connected'){
            showMyFriends();
            FB.api('/me/permissions',function(response){
                console.log(response);
            });
        } else if (response.status === 'not_authorized'){
            login();
        } else {
            login();
        }
    });


    };
    function login(){
        FB.login(function(response){
            if (response.authResponse){
                alert('login');
            } else {
                alert('cancelled'); 
            }
        },{scope:'read_stream'});

    }

    function showMyFriends(){
        FB.api('/me/friends',function(response){
            if(response.data){
                var length = response.data.length;
                for(var i=0;i<50;i++){
                    element=response.data[i];
                    getAFriendStatus(element.id);
                }
            }
        });
    }

    function getAFriendStatus(friendUID){
        FB.api('/fql',{q:{"query1":"select message from status where uid="+friendUID}},
            function(response){
                console.log(response);
            }
        );
    }



  // Load the SDK's source Asynchronously
  // Note that the debug version is being actively developed and might 
  // contain some type checks that are overly strict. 
  // Please report such bugs using the bugs tool.
  (function(d, debug){
     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 = "https://connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
     ref.parentNode.insertBefore(js, ref);
   }(document, /*debug*/ false));
</script>
</body>

当我检查控制台时,每次拨打getAFriendStatus()的电话都是:

Object {data: Array[1]}
data: Array[1]
0: Object
fql_result_set: Array[0]
name: "query1"
__proto__: Object
length: 1
__proto__: Array[0]
__proto__: Object

因为fql_result_set的大小为0,我认为我的查询存在问题。我不认为这是权限问题,因为FB.api('/me/permissions')的控制台输出是:

Object {data: Array[1], paging: Object}
data: Array[1]
0: Object
basic_info: 1
installed: 1
read_stream: 1

所以我知道我有read_stream的许可。为什么我没有获得此查询的状态?

编辑:我刚用

尝试了FQL查询
"select message from status where uid=me()"

我收到了我最近的所有状态帖子。这似乎只是一个问题,让我的朋友的状态?这可能是由于他们的隐私设置?

1 个答案:

答案 0 :(得分:0)

这对我有用,我正在使用

select message from status where uid = XXX

给出了与使用Graph API相同的结果

XXX?fields=statuses

由于您只要求“read_stream”,因此您还需要为朋友信息添加权限。您需要的额外许可是

friends_status

没有它,你就得到了朋友UID