1
votes

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.

4
Did you try using the Firebug console to check out for whats going wrong with the script? - Viren Rajput
Are there any errors popping out in the Console? - Viren Rajput
There were no errors logged at all. The thing is, the code could obviously get the user's name, but when it comes to the likes it just doesn't return any value. - J Armaggedon
oops, definitely no errors at all since I'm using an account that was able to check it's page likes. I may need to get hold of my friend's account later as his account is experiencing this scenario. - J Armaggedon
Have to tried using the Graph API Explorer to make sure there is indeed an issue? developers.facebook.com/tools/explorer - Nick

4 Answers

0
votes

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

0
votes

This can be related to this reproducable bug: http://developers.facebook.com/bugs/106991419491608 Read my post there for more info.

0
votes

I posted a detailed solution to a similar issue I faced with the JS api in this post: Why is the Facebook API returning an empty array for albums?

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