简体   繁体   中英

Javascript check if user already liked facebook page or not

In my website, when the visitor visit my web, if they have not liked my facebook page yet, i want to show Iframe like box. I have seen many code in the web as well as in stackoverflow, but it seem doesn't work well. Here is my javascript code:

$(document).ready(function(){
  FB.init({
    appId  : 'IDAPP',
    status : true,
    cookie : true,
    xfbml  : true
  });

  FB.getLoginStatus(function(response) {
    if (response.status == 'connected') {
        var user_id = response.authResponse.userID;
        var page_id = "PAGE_ID";
        var fql_query = "SELECT uid FROM page_fan WHERE page_id =" + page_id + " and uid =" + user_id;
        var the_query = FB.Data.query(fql_query);

        the_query.wait(function(rows) {
          alert(rows);
            if (rows.length == 1 && rows[0].uid == user_id) {
                alert("like");

            } else {
                alert("not like");
            }
        });

    } else if (response.status === 'not_authorized') {
       alert("not authorized");

    } else {
       alert("not login");
    }
  });
});

Request with login or not login or not_authorize it work well, but when check for page is like or note, it doesn't run anything.

FQL is deprecated, and you would need to authorize a user with the user_likes permission - after that, it´s just a call to the /me/likes endpoint. You will not get that permission approved for like gating, because that´s not allowed. And you will not get it approved for "please like my page" overlays/iframes either :) - because the user does not benefit from that overlay in any way, it is only annoying.

There is only one possible way:

Problem is, you don´t get the information if the liked or unliked something. And the user could just clear his cookies, so you can never be sure. My advice: don´t annoy your users with it.

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