Check if logged in user has liked my Facebook Page

▼魔方 西西 提交于 2019-12-17 22:10:01

问题


OK. So I have no idea on how to do this but you are going to have to talk like a 3 year old to me. I know PHP, HTML, CSS and a tiny smidge of javascript. I want to be able to make a page that gets the users facebook and checks to see if they have liked my Facebook page (https://www.facebook.com/MrDarrenGriffin). If they have, a redirect will direct them to a page. However, If not, it will tell them that to go to the downloads page, they have to like the page. The liking method could be with the embedded like button in the code. After they have liked the page it will go through the check again and if they have successfully liked my page, it will redirect them to the downloads section (or a specified page).

Thanks in advance :D


回答1:


Here are the steps to do so:

1) You need to use this step by step guide to connect user to your facebook page in order to fetch user basic information

https://developers.facebook.com/docs/facebook-login/getting-started-web/

FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
}

2) After knowing that user is connected using facebook you need to issue the graph GET request to find out about this user that he liked your page or not

FB.api('/me/likes/YOUR_APP_ID', function(response) {
    console.log(response.data);
}

3) And then run your business logic (whether to take user to download page or other page)

Code from the tutorial above is given below too

<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
  FB.init({
  appId      : 'YOUR_APP_ID', // App ID
  channelUrl : '//www.example.com/channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});

FB.Event.subscribe('auth.authResponseChange', function(response) {
    if (response.status === 'connected') {
      testAPI();
    } else if (response.status === 'not_authorized') {
  $("#btnFB").show();     
      FB.login();
    } else {
  $("#btnFB").show();         
      FB.login();
    }
});

};

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

function testAPI() {
  FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
  });
  FB.api('/me/likes/PAGE_ID', function(response) {
    console.log(response.data);
  });
}

How to get PAGE_ID? Goto http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Flikes%2F

This worked for me! :)




回答2:


You can use this code because it's so simple :

FB.api({
    method:     "pages.isFan",
    page_id:        page_id,
},  function(response) {
        console.log(response);
        if(response){
            alert('You Likey');
        } else {
            alert('You not Likey :(');
        }
    }
);

this ll user user_likes permission

Hope this will help you



来源:https://stackoverflow.com/questions/17042350/check-if-logged-in-user-has-liked-my-facebook-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!