我正在嘗試Facebook服務器端登錄(使用PHP),並且除了顯示我的名字外,一切似乎都起作用。所以,我一直在遵循這個指南:https://developers.facebook.com/docs/howtos/login/server-side-login/,一切都像寫在那裏一樣。我已經過去並刪除了所有內容,以確保它是正確的。所以當我登錄到我的網站時,我有時會得到「狀態不匹配,你可能是CSRF的犧牲品」,但是我總是得到Hello。你好之後就是我的名字應該在的地方,但事實並非如此。所以很明顯,登錄和授權部分工作,但它不會顯示我的名字。你們有誰知道可能是錯的?使用Facebook登錄用戶服務器端登錄不起作用
這裏是我的代碼:
<?php
$app_id = "MY_APP_ID";
$app_secret = "MY_APP_SECRET";
$my_url = "MY_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'] . "&scope=user_birthday,read_stream";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['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);
$_SESSION['access_token'] = $params['access_token'];
$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.");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Personal testing site</title>
</head>
<body>
<p>This is a personal testing site, and is not related to *mysite* or any of their upcoming functions/ products. </p>
</body>
</html>
編輯1:
我想我可能已經發現了其中的錯誤,但我仍然不知道如何解決它。所以,當我按照代碼工作並試圖回顯代碼的特定部分時,我發現access_token是空的。所以,$ token_url的作品(我認爲),但當我試圖回聲$迴應或$ _SESSION ['access_token']他們都是空白。當我試圖回顯出$ graph_url時,我得到「https://graph.facebook.com/me?access_token=」,沒有任何背後的token =,其中access_token應該被連接在上面。我不知道這是連接問題,還是訪問令牌爲空,但我認爲它是最後一個。我一次又一次地檢查我的代碼,當我將它與Facebook的網頁上的代碼進行比較時,沒有任何問題。
認爲你們可以找到任何東西?
編輯2:
當我回聲$ TOKEN_URL在我的網頁瀏覽器進入它,我得到這個錯誤信息:
{
"error": {
"message": "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request",
"type": "OAuthException",
"code": 100
}
}
你們認爲這可能有什麼用事實上,Facebook使用https,但我的網站只使用http?
編輯3:
這裏是我的應用程序設置的圖片: * Deleted_Picture *
編輯4:
好了,我已經解決了。問題是,應用程序設置中的url必須具有最後一個/,並且我的代碼中的$ my_url必須與此完全相同。
但是,我遇到了更多的問題。看起來像ØØÅ這樣的字母會在某處變形,並且Ø(這是我的姓氏)變成(Ã)(沒有括號)。有誰知道如何解決這一問題?
你檢查過'json對象'有一個名爲'name'的索引嗎? – 2013-02-19 18:46:13
嗨!我只是從他們的網頁上覆制了一切。所以我猜它有:P – Aleksander 2013-02-19 18:57:54