javascript facebook检查用户是否是页面的粉丝

时间:2013-04-09 13:16:39

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

这是我的代码:

    <div id="fb-root"></div>

<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : '',                        // App ID from the app dashboard
      channelUrl : 'http://.php', // Channel file for x-domain comms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
    FB.api('me/likes/',function(response) {
        if( response.data ) {
            if( !isEmpty(response.data) )
                alert('You are a fan!');
            else
                alert('Not a fan!');
        } else {
            alert(response);
            alert('ERROR!');
        }
    });
  };

  // Load the SDK asynchronously
  (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/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));



// function to check for an empty object
function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
</script>

但是我已经返回错误字符串......我做错了什么?

2 个答案:

答案 0 :(得分:9)

要获得用户是否是该页面的粉丝,您可以这样做:

FB.api({
    method:     'fql.query', 
    query:  'SELECT uid FROM page_fan WHERE uid=user_id AND page_id=page_id'
}, function(resp) {
    if (resp.length) {
        alert('A fan!')
    } else {
        alert('Not a fan!');
    }
}
);

您可以在此处详细了解:http://www.masteringapi.com/tutorials/facebook-api-check-if-a-user-is-fan-of-a-facebook-page/20/

类似的问题:

Facebook Javascript, How to detect if user is a fan of my facebook page? While on my site?

答案 1 :(得分:-1)