0

In the facebook application that I built using the VS2012 template I do not want to have a facebook permissions request.

The app is meant to act as a fan gate for the facebook page. So I adapted the code below with my app specific settings. This code is from How to check if a user likes my Facebook Page or URL using Facebook's API. But now the app pops up a facebook permissions check to allow access to likes.

Is there another technique to check likes that will work in the environment I am using?

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <style type="text/css">
      div#container_notlike, div#container_like {
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function () {
            FB.init({
                appId: 'YOUR_APP_ID', // App ID
                channelUrl: 'http(s)://YOUR_APP_DOMAIN/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.getLoginStatus(function (response) {
                var page_id = "YOUR_PAGE_ID";
                if (response && response.authResponse) {
                    var user_id = response.authResponse.userID;
                    var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + user_id;
                    FB.Data.query(fql_query).wait(function (rows) {
                        if (rows.length == 1 && rows[0].uid == user_id) {
                            console.log("LIKE");
                            $('#container_like').show();
                        } else {
                            console.log("NO LIKEY");
                            $('#container_notlike').show();
                        }
                    });
                } else {
                    FB.login(function (response) {
                        if (response && response.authResponse) {
                            var user_id = response.authResponse.userID;
                            var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + user_id;
                            FB.Data.query(fql_query).wait(function (rows) {
                                if (rows.length == 1 && rows[0].uid == user_id) {
                                    console.log("LIKE");
                                    $('#container_like').show();
                                } else {
                                    console.log("NO LIKEY");
                                    $('#container_notlike').show();
                                }
                            });
                        } else {
                            console.log("NO LIKEY");
                            $('#container_notlike').show();
                        }
                    }, { scope: 'user_likes' });
                }
            });
        };

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

    <div id="container_notlike">
      YOU DON'T LIKE ME :(
    </div>

    <div id="container_like">
      YOU LIKE ME :)
    </div>

  </body>
</html>
Community
  • 1
  • 1
tab
  • 63
  • 1
  • 9
  • 2
    Without asking for permission, it is only possible to check if the user liked a Facebook fan page from within a page tab app (keyword: `signed_request`). From _anywhere else_ resp via API it is not possible without asking for `user_likes` permission first. – CBroe Apr 05 '13 at 09:46

1 Answers1

4

AS CBroe said, the only way to know if the user likes your page, without asking for the likes permission, it's using the signed request on page tabs.

If your app is a Page Tab, on your index file a signed request is posted.

The signed_request is posted as json object this is the usual signed request:

{
   "oauth_token": "...big long string...",
   "algorithm": "HMAC-SHA256",
   "expires": 1291840400,
   "issued_at": 1291836800,
   "registration": {
      "name": "Paul Tarjan",
      "email": "fb@paulisageek.com",
      "location": {
         "name": "San Francisco, California",
         "id": 114952118516947
      },
      "gender": "male",
      "birthday": "12/16/1985",
      "like": true,
      "phone": "555-123-4567",
      "anniversary": "2/14/1998",
      "captain": "K",
      "force": "jedi",
      "live": {
         "name": "Denver, Colorado",
         "id": 115590505119035
      }
   },
   "registration_metadata": {
      "fields": "[\n {'name':'name'},\n {'name':'email'},\n {'name':'location'},\n {'name':'gender'},\n {'name':'birthday'},\n {'name':'password'},\n {'name':'like',       'description':'Do you like this plugin?', 'type':'checkbox',  'default':'checked'},\n {'name':'phone',      'description':'Phone Number',             'type':'text'},\n {'name':'anniversary','description':'Anniversary',              'type':'date'},\n {'name':'captain',    'description':'Best Captain',             'type':'select',    'options':{'P':'Jean-Luc Picard','K':'James T. Kirk'}},\n {'name':'force',      'description':'Which side?',              'type':'select',    'options':{'jedi':'Jedi','sith':'Sith'}, 'default':'sith'},\n {'name':'live',       'description':'Best Place to Live',       'type':'typeahead', 'categories':['city','country','state_province']},\n {'name':'captcha'}\n]"
   },
   "user_id": "218471"
}

If you parse this json object into an array and if your app is a Page Tab By doing this, signed_request['page']['liked'] you'll get TRUE if the user likes your page or FALSE if he doesn't.

You can read more about parsing a sign requeste here: https://developers.facebook.com/docs/howtos/login/signed-request/

And more about specific fields for each app type, here: https://developers.facebook.com/docs/reference/login/signed-request/

Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
  • Thanks for your answer. I found more information on the technique you suggested in my ASP .Net [link](http://stackoverflow.com/questions/7891303/decode-signed-request-without-authentication). Hope to be able to test it today. – tab Apr 08 '13 at 04:10