Well, this is continuing from my previous post on Facebook like gating.
This was a pain and a lot of Google-ing later I managed to find out why you can’t just do this to the Facebook signed_request user info variable;
<cfset a = listToArray(form.signed_request, ‘.’) />
#toBinary(a[2])#
This throws an error, because Facebook encode using base64url
So I had to code my own way of doing it which involves something that took me a fair while to work out, I had to pad out the encoded string. Annoying. See the code below!
<cfscript>
//Decode the signed request
fb_str = listToArray(FORM.signed_request, '.');
//Facebook use strtr in PHP, this does the same thing
fb_str = replacelist(fb_str[2], "-,_", "+,/");
//For some reason their base64 needs padding out to match a base64 length
pad = repeatstring("=", 4-len(fb_str) mod 4);
//Decode it
result = ToString(BinaryDecode(fb_str & pad, 'base64'));
//JSON-ify it
liked = deserializeJSON(result);
//Clean up and release memory
fb_str = pad = result = '';
</cfscript>
Enjoy!
