[英]Facebook Authentication PHP & AJAX
我有一個網站,該網站首先使用Facebook Javascript API登錄用戶。但是,當用戶向我的一個PHP腳本發送xmlhttp請求時,我想再次檢查該用戶是否通過PHP登錄(對於證券)。
我有一個運行中的系統,但是在站點改造之后,似乎有一個錯誤。 當我在facebook.php腳本中回顯$ html時,出現以下錯誤: {"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException"}}
。
/* request.js */
var postData = "id=" + id;
sendRequest('assets/php/believe.php',function(req) {
console.log(req.responseText);
},postData);
/* believe.php */
<?php
include("facebook.php");
$id = intval($_POST["id"]);
if($id == '') {
//Stuff
} else {
if($cookie) {
echo "hey";
}
}
?>
/* facebook.php */
<?php
define('YOUR_APP_ID', 'xxxxxx');
define('YOUR_APP_SECRET', 'xxxxxxxxxxxx');
function curl_get_contents($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$html = curl_exec($curl);
curl_close($curl);
echo "$html";
return $html;
}
function get_facebook_cookie($app_id, $app_secret){
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value){
if ($key != 'sig'){
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']){
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$user = json_decode(curl_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']));
?>
我認為您的代碼很好。 用戶訪問令牌已過期。 如果用戶更新它,該代碼將起作用。 確保您在Cookie中具有有效令牌
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.