您好,我試圖使用Facebook服務器端的身份驗證來獲取訪問令牌,但不斷收到我的錯誤。Facebook訪問令牌 - 服務器端身份驗證
我現在用的波紋管:
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
的App ID和APP的祕密已被刪除,所以不要擔心部分。 我得到有關get_file_contents超時的錯誤。
[function.file-get-contents]: failed to open stream: Connection timed out
是否有另一種或更好的獲取訪問令牌的方法?
貝婁是版本我都試過,包括捲曲的想法:
function get_data_cURL($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function getAccessToken(){
$app_id = FB_APP_ID;
$app_secret = FB_SECRET_ID;
$canvas_URL = FB_CANVAS_URL;
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_URL) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_URL)
. "&client_secret=" . $app_secret . "&code=" . $code;
$returned_content = get_data_cURL($token_url);
echo'Well Done! - '.$returned_content;
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
}
詳情:
應用程序設置 - 帆布網址:http://www.apps.elistone.co.uk/DrawMyFriend/
FB_APP_ID = The Facebook App ID
FB_SECRET_ID = The Facebook Secret ID
FB_CANVAS_URL = http://www.apps.elistone.co.uk/DrawMyFriend/
即使此更新我仍然得到這個錯誤:
舊的錯誤
Well Done! - {"error":{"message":"Error validating verification code.","type":"OAuthException","code":100}}
新的錯誤
這似乎被重定向移除狀態代碼引起的。
The state does not match. You may be a victim of CSRF. - 193c791ddd71c3fd84d411db5554c315 (state code)
我也試圖通過改變繞過CSRF保護:
if($_REQUEST['state'] == $_SESSION['state'])
TO:
if(!empty($code))
但是,這仍然使舊的錯誤問題,我聽說它是造成在重定向鏈接的東西,但我不知道如何解決它。
問題解決了
沿着時間嘗試我已經導致使用以下,以獲取用戶訪問令牌使用PHP SDK後:
$app_id = FB_APP_ID;
$app_secret = FB_SECRET_ID;
$canvas_URL = FB_PAGE_URL;
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . $canvas_URL . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$facebook->api('oauth/access_token', array(
'client_id' => FB_APP_ID,
'client_secret' => FB_SECRET_ID,
'type' => 'client_cred',
'code' => $code,
));
$token = $facebook->getAccessToken();
echo$token;
}
現在我準備把它變成一些sorta函數。 非常感謝大家的時間和幫助。
希望這有助於有需要的人,將來在這個步驟(除非Facebook的突然改變了一切)失敗的
任何你不想使用Facebook PHP SDK的原因? – phwd
@phwd爲什麼我不使用PHP SDK的原因是因爲這是給我的應用程序訪問令牌不是用戶訪問令牌,這是我需要做我想做的事情。 –