javascript - Check if user liked page or not - Stack Overflow

I have read a lot of related topics here regarding this problem but I am still not able to do something

I have read a lot of related topics here regarding this problem but I am still not able to do something like this

Below is the code i am using

here are my questions.

  • So I am already logged in into my facebook page
  • If I go to the above dominos url, i can like or unlike the page. The dominos content changes based on whether I have liked or unlike the page. But they are not asking we to log in again
  • In my code below although I already logged in my facebook. i don't get access to my userinfo until I click login button and login in again in the provided popup. how can I skip this step as the dominos is doing. I am getting response.status as not_authorized

    • I know once I log in, I can do something like to check whether the user has liked my page or not

    var query = FB.Data.query( 'select page_id from page_fan where uid='+response.authResponse.userID+' and page_id ='+PAGE_ID);

Here is the code

<!DOCTYPE html>
<html>
<head>
  <title>Fbjquery</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
<div id="fb-root"></div>
<h2>Updated JS SDK example</h2><br />
<div id="user-info"></div>
<p><button id="fb-auth">Login</button></p>


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

  function updateButton(response) {
    var button = document.getElementById('fb-auth');
        console.log("inside update button "+response.authResponse);
    if (response.authResponse) {
        console.log(response.authResponse);
      //user is already logged in and connected
      var userInfo = document.getElementById('user-info');
      FB.api('/me', function(response) {
        userInfo.innerHTML = '<img src="/' 
      + response.id + '/picture">' + response.name;
        button.innerHTML = 'Logout';
      });
      button.onclick = function() {
        FB.logout(function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML="";
    });
      };
    } else {
      //user is not connected to your app or logged out
      button.innerHTML = 'Login';
      button.onclick = function() {
        FB.login(function(response) {
      if (response.authResponse) {
            FB.api('/me', function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML = 
                '<img src="/' 
            + response.id + '/picture" style="margin-right:5px"/>' 
            + response.name;
        });    
          } else {
            //user cancelled login or did not grant authorization
          }
        }, {scope:'email'});    
      }
    }
  }

  // run once with current status and whenever the status changes

  FB.getLoginStatus(updateButton);
  FB.Event.subscribe('auth.statusChange', updateButton);    


};

(function() {
  var e = document.createElement('script'); e.async = true;
  e.src = document.location.protocol 
    + '//connect.facebook/en_US/all.js';
  document.getElementById('fb-root').appendChild(e);
}());

</script>

</body>
</html>

I have read a lot of related topics here regarding this problem but I am still not able to do something like this

http://www.facebook./Dominos/app_252492331452398

Below is the code i am using

here are my questions.

  • So I am already logged in into my facebook page
  • If I go to the above dominos url, i can like or unlike the page. The dominos content changes based on whether I have liked or unlike the page. But they are not asking we to log in again
  • In my code below although I already logged in my facebook. i don't get access to my userinfo until I click login button and login in again in the provided popup. how can I skip this step as the dominos is doing. I am getting response.status as not_authorized

    • I know once I log in, I can do something like to check whether the user has liked my page or not

    var query = FB.Data.query( 'select page_id from page_fan where uid='+response.authResponse.userID+' and page_id ='+PAGE_ID);

Here is the code

<!DOCTYPE html>
<html>
<head>
  <title>Fbjquery</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
<div id="fb-root"></div>
<h2>Updated JS SDK example</h2><br />
<div id="user-info"></div>
<p><button id="fb-auth">Login</button></p>


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

  function updateButton(response) {
    var button = document.getElementById('fb-auth');
        console.log("inside update button "+response.authResponse);
    if (response.authResponse) {
        console.log(response.authResponse);
      //user is already logged in and connected
      var userInfo = document.getElementById('user-info');
      FB.api('/me', function(response) {
        userInfo.innerHTML = '<img src="https://graph.facebook./' 
      + response.id + '/picture">' + response.name;
        button.innerHTML = 'Logout';
      });
      button.onclick = function() {
        FB.logout(function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML="";
    });
      };
    } else {
      //user is not connected to your app or logged out
      button.innerHTML = 'Login';
      button.onclick = function() {
        FB.login(function(response) {
      if (response.authResponse) {
            FB.api('/me', function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML = 
                '<img src="https://graph.facebook./' 
            + response.id + '/picture" style="margin-right:5px"/>' 
            + response.name;
        });    
          } else {
            //user cancelled login or did not grant authorization
          }
        }, {scope:'email'});    
      }
    }
  }

  // run once with current status and whenever the status changes

  FB.getLoginStatus(updateButton);
  FB.Event.subscribe('auth.statusChange', updateButton);    


};

(function() {
  var e = document.createElement('script'); e.async = true;
  e.src = document.location.protocol 
    + '//connect.facebook/en_US/all.js';
  document.getElementById('fb-root').appendChild(e);
}());

</script>

</body>
</html>
Share Improve this question edited Mar 24, 2012 at 12:49 user1025488 asked Mar 24, 2012 at 8:39 user1025488user1025488 2213 silver badges11 bronze badges 1
  • Related/dup: stackoverflow./questions/6246449/… – ripper234 Commented Oct 23, 2012 at 15:39
Add a ment  | 

2 Answers 2

Reset to default 3

You can do this in a programmatic way as well, not necessarily just in the context of a tab.

Note that it will require you to ask for the "user_likes" permission from the user in your O-Auth connect dialog.

This code snippet will test for whether someone currently likes something or not:

    FB.api('/me/likes/MY_PAGE_ID', {limit: 1}, function(r) { 
        if (r.data.length == 1) {
            //do stuff when the user is a liker
        } else {
            //do stuff when the user is not currently a liker           
        }
    });

If you want to catch the event when the user clicks the like button, then you can use FB.Event.subscribe:

    FB.Event.subscribe('edge.create',
         function(response) {
              //Do stuff when the user just clicked a "like" button
         }
    );

On a Facebook fan page, when the user clicks the Like button, the whole page gets reloaded and Facebook sends an HTTP post to your website with a parameter called signed_request that you would need to decode and look at with server code. Signed Request is documented here. Once decoded, you will need to look at the page.liked value.

Might be helpful:

Fans-only content in facebook with asp C# sdk

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744371916a4570996.html

相关推荐

  • javascript - Check if user liked page or not - Stack Overflow

    I have read a lot of related topics here regarding this problem but I am still not able to do something

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信