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.
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)
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).
"liked":true, // field to decide if user has "Liked" the fan page or not
With the above JSON, we can see if the user has “Liked” the Fan Page or Not.
This worked for me.
Hope it helps