9

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

slugster
  • 49,403
  • 14
  • 95
  • 145
  • In that case i would make another method of getting the content. –  Jun 11 '13 at 10:55
  • In that case, all the facebook users could use that, too. So effectively you'd say "if you like my stuff, please click that like button". So why don't you do just that? – thejh Jun 11 '13 at 10:57
  • 1
    I came here expecting code, not a lecture, sir. I have other methods of unlocking content up my sleve, but this one im stuck on. –  Jun 11 '13 at 10:57
  • Would you like to create an app, ask user to authorize you and let you check if he has liked the page or are you thinking of doing the same without authorization ? – Anvesh Saxena Jun 11 '13 at 11:18
  • It's a page. I want it to check if the user logged in has liked it and if so it will redirect to a page, else, it will prompt them to like it –  Jun 11 '13 at 11:19
  • Within a page tab app you get the info whether or not the user viewing it has liked the page or not in the `signed_request` parameter. In _any other situation_ you will have to have the user login to your app and give you permission to read their likes. – CBroe Jun 11 '13 at 11:55
  • I would like it hosted on my site –  Jun 11 '13 at 11:56

2 Answers2

14

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

simyos
  • 305
  • 1
  • 3
  • 10
9

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! :)

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
wakqasahmed
  • 2,059
  • 1
  • 18
  • 23
  • Also here is another idea not to tease returning users by automatically logged them in to our application. http://developers.facebook.com/blog/post/2012/05/08/how-to--improve-the-experience-for-returning-users/ – wakqasahmed Jun 11 '13 at 15:23
  • 1
    does this thing still work ? Because the facebook has a policy of not issuing read_likes access tokens to people .. ? – Hammad Haleem Sep 04 '14 at 16:20
  • Facebook bans incentivizing users to Like Pages, gives developers until November 5 to update apps. So till then it should be working http://thenextweb.com/facebook/2014/08/07/facebook-bans-incentivizing-users-like-pages-gives-developers-november-5-update-apps/ – wakqasahmed Sep 07 '14 at 11:00
  • Please note that using user_likes to check if someone has liked your app or feature will not be approved by Facebook – KarthikKPN Sep 19 '17 at 12:03