How to check if a user likes my Facebook Page or URL using Facebook's API

后端 未结 5 1115
囚心锁ツ
囚心锁ツ 2020-11-22 02:37

I think I\'m going crazy. I can\'t get it to work.
I simply want to check if a user has liked my page with javascript in an iFrame app.

FB.a         


        
5条回答
  •  轮回少年
    2020-11-22 03:35

    Though this post has been here for quite a while, the solutions are not pure JS. Though Jason noted that requesting permissions is not ideal, I consider it a good thing since the user can reject it explicitly. I still post this code, though (almost) the same thing can also be seen in another post by ifaour. Consider this the JS only version without too much attention to detail.

    The basic code is rather simple:

    FB.api("me/likes/SOME_ID", function(response) {
        if ( response.data.length === 1 ) { //there should only be a single value inside "data"
            console.log('You like it');
        } else {
            console.log("You don't like it");
        }
    });
    

    ALternatively, replace me with the proper UserID of someone else (you might need to alter the permissions below to do this, like friends_likes) As noted, you need more than the basic permission:

    FB.login(function(response) {
        //do whatever you need to do after a (un)successfull login         
    }, { scope: 'user_likes' });
    

提交回复
热议问题