Perl& Facebook json signed_request解析

时间:2011-12-05 10:52:56

标签: facebook json perl

我正在尝试解析你在perl从Facebook收到的json signed_request,但需要一些帮助!

($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>";
}
}

每个$值可以包含一个字符串或对另一个HASH的引用(其中一些HASH可能包含另一个HASH),有没有更好的方法来处理它?<​​/ p>

上面的脚本返回:

algorithm = HMAC-SHA256
page = HASH(0x632b100) 
issued_at = 1323081670 
user = HASH(0x632b150) 

和解码后收到的json是:

{"algorithm":"HMAC-SHA256","issued_at":1323081670,"page":{"id":"192130540873448","liked":true,"admin":true},"user":{"country":"gb","locale":"en_GB","age":{"min":21}}}

理想情况下,我希望看到以下结果:

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 

数据结构将随着来自Facebook的授权用户响应而增加,提供更多数据。

非常感谢

3 个答案:

答案 0 :(得分:1)

最初我发布了一个带有工作示例的网址。感谢Brad指出最好在这里提供代码,所以我删除了一个URL并包含以下代码:

#!/usr/bin/perl

use strict;
use CGI;
use MIME::Base64;
use JSON;
use Digest::SHA qw(hmac_sha256);

my $secret = '<secret>';

my $q = new CGI;
my $sreq = $q->param('signed_request');

eval {
    die 'not a signed_request' unless (defined $sreq);
    my ($esig, $pload) = split('\.', $sreq);
    $esig = decode_base64url($esig);
    my $vsig = hmac_sha256($pload, $secret);
    die 'invalid signature' unless ($esig eq $vsig);
    my $data = decode_json(decode_base64url($pload));
    print "Content-Type: text/html\n\n";
    if ($data->{'page'}->{'liked'} eq 'true') {
        print "Oh yeah, you liked me :-)";
    } else {
        print "No? why not? Please press the Like button";
    }
};
if ($@) {
    print "Content-Type: text/html\n\ndied: $@";
}

sub decode_base64url {
    my $s = shift;
    $s =~ tr[-_][+/];
    $s .= '=' while length($s) % 4;
    return decode_base64($s);
}

sub parse_hash {
    my $hash = shift;

    my $array = [];
    foreach my $key (keys %$hash) {
        if (ref($hash->{$key}) eq 'HASH') {
            push @$array, $key . ' => {' . join(', ', @{ parse_hash($hash->{$key}) }) . '}';
        } else {
            push @$array, $key . ' => ' . $hash->{$key};
        }
    }
    return $array;
}

答案 1 :(得分:0)

回答我自己的问题:)

if ($expected_sig eq $sig) {
while (my ($key, $value) = each %decoded) { 
 if (ref($value) eq "HASH") {
 while ( my ($key2, $value2) = each(%{$value}) ) {
$mydata{"$key-$key2"}=$value2;
 if (ref($value2) eq "HASH") {
  while ( my ($key3, $value3) = each(%{$value2}) ) {
$mydata{"$key-$key2-$key3"}=$value3;
        print "$key-$key2-$key3 = $value3\n<br>";
}}
else
{
        print "$key-$key2 = $value2\n<br>";
}}}
else
{ 

$mydata{"$key"}=$value;
print "$key = $value\n<br>";
}}}

输出:

algorithm = HMAC-SHA256 
page-admin = 1 
page-liked = 1 
page-id = 192130540873448   
issued_at = 1323081670    
user-country = gb 
user-locale = en_GB 
user-age-min = 21 

答案 2 :(得分:-1)

唯一的方法是:

$my_algorithm = $algorithm
$page-id = $page->{'id'};
$page-liked = $page->{'linked'};
$page-admin = $page->{'admin'};
$my_issued_at = $issued_at
$user-country = $user->{'country'};
$user-locale = $user->{'locale'};
$user-age-min = $user->{'age'}->{'min'};