I’m trying to parse the json signed_request you receive from Facebook in perl but need a little help!
($encoded_sig, $payload) = split('\.', $formdata{'signed_request'});
$sig = decode_base64url($encoded_sig);
$data = decode_json(decode_base64url($payload));
%decoded = %{ decode_json(decode_base64url($payload)) };
$expected_sig = hmac_sha256($payload, $app_secret);
if ($expected_sig eq $sig) {
while (my ($key, $value) = each %decoded) {
print "$key = $value\n<br>";
}
}
Each $value can either contain a string or a reference to another HASH (and some of those HASHs may contain another HASH), is there a better way to process this?
The script above returns:
algorithm = HMAC-SHA256
page = HASH(0x632b100)
issued_at = 1323081670
user = HASH(0x632b150)
and the json received after decoding is:
{"algorithm":"HMAC-SHA256","issued_at":1323081670,"page":{"id":"192130540873448","liked":true,"admin":true},"user":{"country":"gb","locale":"en_GB","age":{"min":21}}}
Ideally would like to see a result of:
algorithm = HMAC-SHA256
page-id = 192130540873448
page-liked = true
page-admin = true
issued_at = 1323081670
user-country = gb
user-locale = en_GB
user-age-min = 21
Data structure will increase as upon an authorised user response from Facebook more data is provided.
Many thanks
To answer my own question 🙂
outputs: