0
我開發與2個文件的應用程序:基於AJAX的應用程序沒有有效的訪問令牌
- 的index.php(應用程序容器)
- process.php(稱爲AJAX)
index.php文件
<?php
include 'config.php';
include 'lib/functions.php';
require_once 'lib/sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => SECRET
));
$userID = $facebook->getUser();
if (!$userID)
{
$scope = 'read_stream, publish_stream, user_likes, user_photos, user_status, friends_hometown, friends_location, friends_likes, photo_upload';
$params = array(
'scope' => $scope,
'redirect_uri' => "https://apps.facebook.com/".APP_NAME."/"
);
$loginUrl = $facebook->getLoginUrl($params);
}
else
{
$access_token = $facebook->getAccessToken();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Facebook App</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link href="/css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body style="margin:0 !important; padding: 0 !important;">
{ SOME HTML }
<script type="text/javascript">
function loadData()
{
$.ajax({
url: "process.php",
type: 'POST',
cache: false,
data: {
id_sessione: function(){return $("#id_sessione").val();}
},
dataType: "json",
success: function(data) {
if (data.status == "FAILURE")
alert('failure');
else
console.log(data);
}
});
}
/********* INIT ********/
$(document).ready(function(){
if ($("#userID").val() != "0")
loadData();
else
top.location = '<?php echo $loginUrl; ?>';
});
</script>
<input id="id_sessione" type="hidden" value="<?php echo session_id(); ?>" />
<input id="userID" type="hidden" value="<?php echo $userID; ?>" />
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo APP_ID; ?>',
frictionlessRequests: true
});
FB.Canvas.setAutoResize(7);
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/it_IT/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
和process.php
<?php
session_id($_POST['id_sessione']);
session_start();
include 'config.php';
include 'lib/functions.php';
require_once 'lib/sdk/facebook.php';
set_time_limit(120);
$memory_limit = ini_set('memory_limit', '512M');
try
{
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => SECRET
));
$userID = $facebook->getUser();
$user_profile = $facebook->api('/me');
{ SOME FQL QUERIES AND DATA PROCESSING }
echo json_encode(array(
'status' => 'SUCCESS',
.... other info
));
} catch (Exception $e) {
$handle = fopen(ROOT_PATH.'/log/'.$userID.'_'.uniqid().'.txt', 'a+') or die('Cannot open file');
fwrite($handle, $e->getMessage()."\n");
fwrite($handle, $e->getTraceAsString()."\n");
fclose($handle);
echo json_encode(array(
'status' => 'FAILURE'
));
}
在95%的案例中,應用程序可以正常工作,但有時候會有$ facebook-> api('/ me'); 「異常」必須使用活動訪問令牌來查詢有關當前用戶的信息「並且$ userID等於0.
爲了在所有文件中必須啓動相同的會話ID通過POST中的index.php傳遞的session_id手動會話。這是因爲某些瀏覽器(IE,safari)不會在iframe中傳遞第三方頁面的cookie。 (http://stackoverflow.com/questions/9930671/safari-3rd-party-cookie-iframe-trick-no-longer-working)
有什麼建議嗎? 謝謝
首先,你有沒有_checked_會話ID是否正確傳遞? – CBroe 2012-07-17 20:56:32