简体   繁体   中英

FB.API Facebook Open Graph returns empty data for user likes for some users

I have a page in which when the app is permitted by a user, it will list down the user's page likes, and checks whether the user liked my certain facebook page and is a member of that facebook page (in a way both are similar), using FB.API

The problem is that certain users are not returning any data at all, regardless of OS or browser. I already set the 'user_likes' permission on the app, but to no avail.

    <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>

I also implemented an FQL which practically does the same thing to check whether a user has liked my page and the same scenario is happening on certain users where data object returns empty rows.

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

I have applied some of the suggestions found here and the script still doesn't work for some users. I am out of clue what particular account/privacy/app setting should be modified to allow querying of the user's page likes, apart from setting the 'user_likes' in the App's user and friend permissions settings.

You've given no indication how often this is happening so i'm assuming

  • It's rare
  • You're only ever asking about the current user, not their friends.

If so, some users don't actually have any likes, so this can easily happen and shouldn't be a problem.

If you can verify the user DOES have likes, and has authorised your app with user_likes permission, file a bug in Facebook's bug tracker and include the user IDs, page IDs, access tokens etc

这可能与该可复制错误有关: http : //developers.facebook.com/bugs/106991419491608在此处阅读我的文章以获取更多信息。

我在这篇文章中针对与JS api相似的问题发布了详细的解决方案: 为什么Facebook API会为相册返回一个空数组?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM