【发布时间】:2012-03-08 12:52:54
【问题描述】:
我正在关注official tutorial 来实施 FB 积分,但它不起作用。
我添加了警报语句以确保代码正在执行,从警报消息中,我确定没有 js 错误并且正在调用 FB.ui。我在回调函数中也有警报消息,但没有收到响应。
我花了 5 个小时才弄清楚代码中的问题。谁能帮帮我。
有关应用程序的其他信息:
- 画布应用
- 未发布(已启用沙盒模式)
- 未注册公司。 FB说我以后可以做所以我刚刚设置了国家。我还没有注册,因为我需要得出一个结论,我需要提供哪些银行账户详细信息,因为 FB 不允许更改它(从界面)
这里是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 : true, // parse XFBML
channelUrl : 'http://localhost/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 = 'myorderinfo';
alert('creating obj');
var obj = {
method: 'pay',
order_info: order_info,
action: 'buy_item',
dev_purchase_params: {'oscif': true}
};
alert('calling ui');
FB.ui(obj, callback);
}
</script>
<input type="button" value="post" onclick="postFeed()" />
<input type="button" value="Buy" onclick="placeOrder()" />
</body>
</html>
如果您注意到 alert 呼叫,我会按顺序收到警报消息
- 'in placeOrder()'
- '创建对象'
- '调用 FB.ui'
回调函数中也有警告消息,但没有被调用
为了确保 fb 正确启动,我实现了提要发布功能并从“postFeedback”的点击事件中调用
function postFeed(){
alert('in postFeed()');
FB.ui(
{
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
这工作正常并在我的墙上发布提要
我还使用https://developers.facebook.com/docs/authentication/signed_request/中给出的示例实现了callback.php
是的,我已经正确配置了应用设置
回调.php
<?php
include_once 'Config.php';
mysql_connect('localhost','root','');
mysql_select_db("precious_world");
//var_dump($_REQUEST);
//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');
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, '-_', '+/'));
}
?>
我在这个文件中有一些额外的代码来转储整个请求以跟踪请求
【问题讨论】:
标签: php facebook facebook-graph-api