检测用户是否喜欢我的粉丝页面

时间:2012-03-30 07:43:01

标签: facebook facebook-like detect

编辑:

经过长时间的搜索后,我找到了解决方法:

继承我的代码:

<html xmlns:fb="http://ogp.me/ns/fb#">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>

        <fb:like href="http://developers.facebook.com" send="true" width="450" show_faces="true"></fb:like>

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

        <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
        <script>
            FB.Event.subscribe('edge.create', function(href, widget) {
                //alert('like '+href);
                document.getElementById('download').style.visibility = 'visible'; 
            });
            FB.Event.subscribe('edge.remove', function(href, widget) {
                //alert('dislike '+href);
                document.getElementById('download').style.visibility = 'hidden'; 
            });
        </script>
        <div id="download">Download</div>
    </body>
</html>

这可以按照我的意愿运行,但是在页面加载时默认会显示下载,如何在页面加载时检测用户是否喜欢我的页面?


旧问题 我有一个网站,我有一些文件供用户下载,旁边有一个像按钮。我只想让当前用户喜欢我的页面时才能看到下载按钮。

我是FB open graph和JS SDK的新手,我尝试使用以下代码(粘贴在我的网站上):

window.fbAsyncInit = function() {
  FB.init({
    appId      : 'XXXXXXXXXXXXXXX',
    status     : true, 
    cookie     : true,
    xfbml      : true,
    oauth      : true,
  });

  FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
    FB.api('me', function(user) {
        if (user) {
          var image = document.getElementById('image');
          image.src = 'http://graph.facebook.com/' + user.id + '/picture';
          var name = document.getElementById('name');
          name.innerHTML = user.name
        }
     });
  } else {
    // the user is logged in to Facebook, 
    // but has not authenticated your app or
    // the user isn't logged in to Facebook.
     FB.login(function(response) {
       if (response.authResponse) {
         FB.api('me', function(user) {
            if (user) {
              var image = document.getElementById('image');
              image.src = 'http://graph.facebook.com/' + user.id + '/picture';
              var name = document.getElementById('name');
              name.innerHTML = user.name
            }
         });
       } else {
         console.log('User cancelled login or did not fully authorize.');
       }
     });
  }
 });
};

(function(d){
   var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   d.getElementsByTagName('head')[0].appendChild(js);
 }(document));

即使我登录FB并使用相同的浏览器,它总是返回我的错误(firebug调试日志)

代码:2500 消息:“必须使用活动访问令牌来查询有关当前用户的信息。”

我对FB API了解不多,我做错了吗?

1 个答案:

答案 0 :(得分:0)

以下是对您的代码的编辑:

    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'XXXXXXXXX',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true,
      });

      FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        // the user is logged in and has authenticated your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire
        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;
        FB.api('me', function(user) {
            if (user) {
              var image = document.getElementById('image');
              image.src = 'http://graph.facebook.com/' + user.id + '/picture';
              var name = document.getElementById('name');
              name.innerHTML = user.name
            }
         });
      } else {
        // the user is logged in to Facebook, 
        // but has not authenticated your app or
        // the user isn't logged in to Facebook.
         FB.login(function(response) {
           if (response.authResponse) {
             FB.api('me', function(user) {
                if (user) {
                  var image = document.getElementById('image');
                  image.src = 'http://graph.facebook.com/' + user.id + '/picture';
                  var name = document.getElementById('name');
                  name.innerHTML = user.name
                }
             });
           } else {
             console.log('User cancelled login or did not fully authorize.');
           }
         });
      }
     });
    };

    (function(d){
       var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
       js = d.createElement('script'); js.id = id; js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       d.getElementsByTagName('head')[0].appendChild(js);
     }(document));

为了解释一下,我添加的是一个功能,用于检查用户是否已经授权您的应用(FB.getLoginStatus),并且在该功能的成功处理程序中,我添加了您的API调用。在此功能的其他内容中,我添加了对FB.login的调用,这将提示未授权用户授权您的应用。如果他们成功地执行了此操作,则会进行API调用,如果他们选择不这样做,我刚刚在示例console.log调用中离开,但您可以将其替换为您想要的任何内容。

现在,为了跟踪用户是否喜欢您的网页,您需要通过跟踪“edge.create”事件来使用FB.Event.subscribe功能。