0

I'd like to integrate the "like" button into my site, but I cannot make sense of the information available on the web. I read this article, which was in another stackoverflow article, but have some questions: http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/. I've also posted this question before and the answers I received really had nothing to do with what I was asking. My original question was asked here: Facebook Integration into website.

I've setup a "Page" already in facebook, and from what I understand in the link above, you need to setup an "App" to get an API key. What I don't understand though is that if I use this API Key, it's going to be pointing to my newly created "App", which has no fan base. How do I link this API Key, (or setup another key through the page admin), so I can have users "like" the real facebook page?

I want to run some javascript functions the moment a user likes the page, but I'm also a little confused on what API functions call, and whether these return a true/false value? I only really want to run these js functions if the user has not already liked the page..

Hope this all makes sense, would love any explanations you have to offer to point me in the right direction.

Community
  • 1
  • 1
Sami.C
  • 561
  • 1
  • 11
  • 24

2 Answers2

0

From what I can see, the answers on your other question cover most of what you need to know. The one thing I notice is your comment:

"I am attempting to set a promo code in the background when someone "likes" the page"

There are some tricky terms and conditions surrounding this. Have a look here before continuing: https://developers.facebook.com/docs/guides/policy/examples_and_explanations/Rewarding_Users/

If you start off with adding the like button, then separately you will need to check each logged in facebook user to see if they are connected with your page. You can do this using the api call to get their likes, and checking for your page id in the response:

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

If you find a match, proceed with your promotion, else show the like button.

Abby
  • 3,169
  • 1
  • 21
  • 40
  • this seems like a reasonably simple task. From what I read about the API though, you require an application ID to use it, is this correct? I am failing to understand the difference between an app and a page.. For the simple task I want to do, do I really need to setup an "app"? If so, am I able to link my existing page to this app or something? I can't see how you do this, and obviously I dont want people to be "liking" some app I create that does nothing, and not going to my page itself. – Sami.C Sep 22 '11 at 01:16
  • You don't need an app id to have a like button. You do need an app id to make queries on the facebook graph, and to use their application program interface. A page is an entity on facebook that users can connect with. An app is an entity on facebook, that users can connect with, and it can connect back to them, and take actions on their behalf; like posting photos and updates etc. If you want to programmatically reward your users, rather than just posting your rewards on the page after they have liked it, then you will need an app – Abby Sep 22 '11 at 07:22
  • so is it possible to link an "app" to my page? I cant see any other way of doing this other than using the API..? – Sami.C Sep 22 '11 at 12:12
  • You can add an app tab to your page – Abby Sep 22 '11 at 12:44
  • awesome, how!? I can't see it anywhere on my facebook page? Are you able to show me a tutorial/link for it? I couldn't find anything on google. Thanks for your help Abby! – Sami.C Sep 22 '11 at 12:54
  • Either you go to edit your page, then on the left click 'Apps', or go to your app page, and on the left click 'Add to my page' – Abby Sep 22 '11 at 13:00
  • ok, i've set up an app, and added it to my page. I'm not sure what "Page Tab URL" is supposed to be, is it supposed to be the URL of my website or my facebook page? Either way I don't see what it is required for. Now I am assuming this means I can connect to the API with the app login details, and I have the ability to query my facebook PAGE. Correct? Also, on my facebook page, there is a link to the "app". This isn't something I want public, is there a way to hide this? I really only want to use the App to connect to the API in the background :( – Sami.C Sep 23 '11 at 02:40
  • This is leaving the scope of your question now. I can't explain the whole process of developing an app. I suggest spending a day or two reading through the developer site https://developers.facebook.com/docs/ which will answer most of your questions – Abby Sep 23 '11 at 08:25
0

What you're trying to do is totally possible, although I usually do that calculation server-side. You may be able to do it via the Javascript SDK, using the basic concept below. Check out this link: http://fbmhell.com/2011/06/facebook-like-gating-in-iframe-tabs/

The basic overview is this:

  1. You create a Page
  2. You create an app for your promo tab
  3. You add the app tab to your page

When the user hits your app tab on your page, Facebook will return a signed request to you.

You can parse out that signed request using a function like this:

function grokSignedRequest() {
    if (isset($_REQUEST['signed_request'])) {
      $encoded_sig = null;
      $payload = null;
      list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
      $sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
      $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
      return $data;
    }
    return false;
}

As mentioned in that article, if you do a print_r() on that signed request after it's been run through the function provided, you’ll see something like this:

stdClass Object
(
    [algorithm] => HMAC-SHA256
    [issued_at] => 1307627872
    [page] => stdClass Object
        (
            [id] => 116633947708
            [liked] => 1
            [admin] => 1
        )

    [user] => stdClass Object
        (
            [country] => us
            [locale] => en_US
            [age] => stdClass Object
                (
                    [min] => 21
                )
        )
)

From there, you can access the liked parameter, and display content based on whether or not the user has liked the page.

// call the function to parse the signed request
$sr_data = grokSignedRequest();

// check like status
if ($sr_data->page->liked==1) {
    echo 'you are a fan';
} else {
    echo 'you are not a fan.';
}

// check admin status
if ($sr_data->page->admin==1) {
    echo '<li>Dude, you are an ADMIN! BADASS!';
}
snipe
  • 517
  • 2
  • 10
  • 23
  • hey mate, is there no function in the javascript API that does this work for us? Surely lots of people have wanted to do this, i can't understand why there isnt a simple function to call for this? Is there any other sample code you have other than php? The site is in ASP and can use Javascript to do any facebook calls. – Sami.C Sep 21 '11 at 12:11
  • You can try looking up the docs in the Javascript SDK: http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ - not sure if "connected" in the context of a fan page tab would count as fan/no fan, but I kinda don't think so. I don't think there is a way to do it via Javascript alone. – snipe Sep 21 '11 at 15:01