FB.API Facebook Open Graph为某些用户返回用户喜欢的空数据

时间:2012-06-21 03:05:02

标签: facebook facebook-graph-api oauth-2.0 facebook-fql

我有一个页面,当用户允许应用程序时,它会列出用户的页面喜欢,并检查用户是否喜欢我的某个Facebook页面并且是该Facebook页面的成员(以某种方式)是类似的),使用FB.API

问题是某些用户根本不返回任何数据,无论操作系统或浏览器如何。我已经在应用上设置了'user_likes'权限,但无济于事。

    <html> 
<head> 
<title>My Facebook Login Page</title> 
</head> 
<body> 
    <div id="fb-root"></div> 
    <div id="lblName"></div> 
    <div id="lblAccessToken" style="display: none;"></div> 
    <br><br> 
    <fieldset> 
        <legend>Your Page Likes</legend> 
        <div id="lblLikes"></div> 
    </fieldset> 

    <fieldset> 
        <legend>Check whether you like [My Fan Page] </legend> 
        <div id="lblYouLike"></div> 
    </fieldset> 

    <fieldset> 
        <legend>Check whether you are a fan of [My Fan Page]</legend> 
        <div id="lblMem"></div> 
    </fieldset> 
    <div id="divLogin" style="display:none;"> 
    <div class="fb-login-button">Login with [App]</div> 
</div> 
<script> 
    var accessToken; 
    var uid; 
    var lblName = document.getElementById("lblName") 
    var divLogin = document.getElementById("divLogin") 
    var lblAccessToken = document.getElementById("lblAccessToken") 
    var lblLikes = document.getElementById("lblLikes") 
    var lblYouLike = document.getElementById("lblYouLike") 
    var lblMem = document.getElementById("lblMem") 

    window.fbAsyncInit = function () { 
        FB.init({ 
            appId: 'APP_ID', // App ID 
            channelUrl : 'http://www.example.com/testing/channel.html', // Channel File 
            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) { 
                checkStatus(response) 
            } 
        ); 

        FB.Event.subscribe( 
            'auth.authResponseChange', 
            function (response) { 
                checkStatus(response) 
            } 
        ); 

        FB.Event.subscribe( 
            'edge.create', 
            function (response) { 
                //alert('You liked the URL: ' + response); 
                ufCheckLike() 
            } 
        ); 
    }; 

    // 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) 
    ); 

    function checkStatus(response){ 
        if (response.status === 'connected') { 
            uid = response.authResponse.userID; 
            accessToken = response.authResponse.accessToken; 

            lblAccessToken.innerHTML = accessToken; 

            FB.api( 
                '/me', 
                'get', 
                function(response) { 
                    lblName.innerHTML = response.name; 
                } 
            ); 

            divLogin.style.display = "none"; 

            ufCheckLike() 
        } 
        else { 
            divLogin.style.display = ""; 
        } 
    } 

    function ufCheckLike() { 
        if (isLoggedIn()) { 
            FB.api( 
                '/me/likes?' + 
                'access_token=' + accessToken, 
                'get', 
                    function(likes) { 
                        if (likes) { 
                            if (likes.data && likes.data.length > 0) { 

                                for (var i = 0; i < likes.data.length; i++) { 
                                    var like_detail = likes.data[i] 
                                    var lsHTML = like_detail.name + " : " + like_detail.id + "<br>" 
                                    if(like_detail.id == "<PAGE_ID>") 
                                        lblLikes.innerHTML = lsHTML + lblLikes.innerHTML 
                                    else 
                                        lblLikes.innerHTML += lsHTML 
                                } 

                            } 
                        } 
                    } 
            ); 

            FB.api( 
                '/me/likes/<PAGE_ID>?' + 
                'access_token=' + accessToken, 
                'get', 
                function(likes) { 
                    if (likes) { 
                        if (likes.data && likes.data.length > 0) { 
                            lblYouLike.innerHTML = "You Like [Fan Page]!" 
                        } 
                    } 
                } 
            ); 

            FB.api( 
                '/<PAGE_ID>/members/' + uid + '?' + 
                'access_token=' + accessToken, 
                'get', 
                function(likes) { 
                    if (likes) { 
                        if (likes.data && likes.data.length > 0) { 
                            lblMem.innerHTML = "You are a fan of [Fan Page]!" 
                        } 
                    } 
                } 
            ); 

        } 

    } 

    function isLoggedIn() { 
        if (!accessToken) { 
            divLogin.style.display = ""; 
            return false; 
        } 
        return true; 
    } 
</script> 
</body> 
</html>

我还实现了一个FQL,它实际上做同样的事情来检查用户是否喜欢我的页面,并且在数据对象返回空行的某些用户上发生了同样的情况。

var query = "SELECT uid FROM page_fan  where uid=me() and page_id = PAGE_ID ' + 'and access_token = " + accessToken;
//Access token is optional
var names = FB.Data.query(query);
names.wait(
    function (rows) {
        if (rows.length > 0) {
            // liked, do codes here
        }
        else {
            // not yet liked, show like button
        }
    }
);

我已经应用了这里找到的一些建议,但脚本仍然不适用于某些用户。我不知道应该修改特定帐户/隐私/应用程序设置以允许查询用户的页面喜欢,除了在应用程序的用户和朋友权限设置中设置'user_likes'。

4 个答案:

答案 0 :(得分:0)

你没有说明这种情况发生的频率,所以我假设

  • 很少见
  • 您只询问当前用户,而不是他们的朋友。

如果是这样,一些用户实际上没有任何喜欢,所以这很容易发生,不应该是一个问题。

如果您可以验证用户是否喜欢,并且已使用user_likes权限授权您的应用,请在Facebook's bug tracker中提交错误并添加用户ID,网页ID,访问令牌等

答案 1 :(得分:0)

这可能与此可重现的错误有关: http://developers.facebook.com/bugs/106991419491608 阅读我的帖子了解更多信息。

答案 2 :(得分:0)

我在这篇文章中发布了一个类似问题的详细解决方案: Why is the Facebook API returning an empty array for albums?

答案 3 :(得分:0)

if(jQuery.type(response.data[i].location) == 'object' )