实施Facebook Credits - 应用程序无响应(回调imeplemntation中的signed_request解析错误)

时间:2012-03-09 11:58:42

标签: php javascript facebook json encoding

这是我的earlier post

的延续

从昨天开始,我一直在尝试按照官方教程实施 facebook学分。可能是我的无聊或信息不足我遇到了很多问题,我已经逐一解决了这个问题,我已经到了最后一步(希望如此)

我想与 order_info 有关,我不确定

我根据给定here的教程创建了一个基本页面。这个页面有一个简单的按钮。单击它会调用pl​​aceOrder()函数,该函数几乎是教程中给出的复制粘贴代码。

现在,我收到此错误消息

enter image description here

callback.php也已实现,似乎FB已经ping了callback.php。但问题似乎是signed_request不符合预期。

我从apache日志

收到此错误消息
  

[Fri Mar 09 11:17:20 2012] [错误] [client 66.220.146.244]未知   算法。预计HMAC-SHA256 但得到了数据转储

注意:(但是获得数据转储是我添加的额外调试变量,用于查找$ data ['algorithm']和$ data完全

的内容

我已经实现了代码转储db 中的signed_request变量,以便进一步调试和调试我调试和跟踪

以下是callback.php的完整代码

<?php

//based on https://developers.facebook.com/docs/credits/callback/


include_once 'Config.php';

mysql_connect('myhost','usr','zzz');
mysql_select_db("mydb");

//dump the request into the db
$request = join(':', $_REQUEST);
$request = mysql_real_escape_string($request);
$query = "insert into fbcredits_callback(data)values('$request')";
$result = mysql_query($query);

$fb_signed_req = $_REQUEST['signed_request'];

echo parse_signed_request($signed_request, Config::$appSecret);

function parse_signed_request($signed_request, $secret) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
   error_log('Unknown algorithm. Expected HMAC-SHA256 but got '.$data['algorithm'].'data dump:'.join(':',$data));
   return null;
  }

  // check sig
  $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  if ($sig !== $expected_sig) {
    error_log('Bad Signed JSON signature!');
    return null;
  }

  return $data;
}

function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}

?>

在此行生成以上错误消息

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
   error_log('Unknown algorithm. Expected HMAC-SHA256 but got '.$data['algorithm'].'data dump:'.join(':',$data));
   return null;
  }

更新:我逐步调试输出,在下面的步骤$ data返回null

 $data = json_decode(base64_url_decode($payload), true);

这意味着解码没有正确发生。有人可以告诉我这里出了什么问题吗?

buy.php的完整代码

<?php 
include_once 'Config.php';
include_once 'fb-sdk/facebook.php';
?>
<html>
    <head>
      <title>My Facebook Credits Page</title>
    </head>
    <body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : '<?php echo Config::$appId?>',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : false, // parse XFBML
    channelUrl : 'http://199.192.xxx.yyy/buy.php', // channel.html file
    oauth  : true // enable OAuth 2.0
  });

var callback = function(data) {
    if (data['order_id']) {
        alert('called back');
      return true;
    } else {
      //handle errors here
      alert('some error');
      return false;
    }
  };

function placeOrder(){

    alert('in placeOrder()');

    var order_info = {
        item_code: "someItemCode",
        user_id: "1313213131"
    };
    alert('creating obj');

    var obj = {
            method: 'pay',
            order_info: order_info,
            action: 'buy_item',
            dev_purchase_params: {'oscif': true},
         app_id: '<?php echo Config::$appId?>'
          };
     alert('calling ui');
     FB.ui(obj, callback);

}

</script>

<input type="button" value="post" onclick="postFeed()" />
<input type="button" value="Buy" onclick="placeOrder()" />
</body>
</html>

其他信息:

  • 我的网络服务器支持SSL(来自verizon的安装测试证书)
  • 启用沙箱模式(尝试禁用)

1 个答案:

答案 0 :(得分:2)

我已经解决了。我再次仔细阅读callback documentation,发现我在做什么错误。

我只是在解析 signed_request 并发回已解析的数据,但我想发回内容

这不是结束,还有更多要做的事情,并在那里有完整的例子。

相关问题