Way to check if user has “Liked” the Facebook Fan Page or Not

22 / Dec / 2011 by Vishal Sahu 2 comments

Hi,
In my recent grails project, i was working on creating Apps for Facebook Fan Page and needed to show data to the user in such a way so that if he/she has “Liked” the page, then the content would be different from the case when the user has not liked it.


In simple words, if user is Fan of any Facebook Page, then the content of App Tab would be different from the user who is not Fan of that Page.


I searched a lot about it, and came across various solutions suggesting using JavaScript way to handle it but they didn’t worked for me, because although the User is logged in the facebook, but has not authorized the App and hence, we cannot fetch user_id of that user from facebook session.


The only way i left with is to fetch the User status from the signed_request served in the params. This is the easiest way i got, to check whether User has ‘Liked’ the page or not, on server side.


A signed_request is passed in params to the application URL registered in App on Facebook when they are loaded into the Facebook environment. It is of the form of long sequence of string concatenation of a HMAC SHA-256 signature string, a period (.), and a base64url encoded JSON object.


Code to decode signed_request


        String signedRequest= SIGNED_REQUEST_OBTAINED_IN_PARAMS
        boolean hasLikedPage = false
        String payload = signedRequest.split("[.]", 2)[1]
        payload = payload.replace("-", "+").replace("_", "/").trim()
        String jsonString = new String(Base64.decodeBase64(payload.getBytes()))

The above code will gives a string containing all the data in the signed_request(as in below example).
Example:

{
   "algorithm":"HMAC- SHA256",
    "expires":1324555200,
    "issued_at":1324551348,
    "oauth_token":"some-random-string-of-oauth-token",
    "page": {
            "id":"XXXXXXXXXXX594",
             "liked":true, // field to decide if user has "Liked" the fan page or not
             "admin":false
          },
    "user":{
            "country":"in",
            "locale":"en_US",
            "age":{
                     "min":21
                    }
              },
    "user_id":"XXXXXXX3285"
}

With the above JSON, we can see if the user has “Liked” the Fan Page or Not.


This worked for me.
Hope it helps


Cheers!!
Vishal Sahu
vishal@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Joby Joseph

    If you are dealing with facebook app tab page, following code help you to find isfan status

    $signed = parse_signed_request($_REQUEST[‘signed_request’], ‘YOUR-APP-SECRET’);
    if ($signed[‘page’][‘liked’] == 1) {
    $fan = true;
    } else {
    $fan = false;
    }

    Reply

Leave a comment -