[英]Why does facebook $user_id = $facebook->getUser(); return 0 in-spite of being logged in?
問題:用戶訪問令牌可從$params['access_token']
但$ facebook-> getUser()返回0。
用戶使用。登錄
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=";
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));
}
else
{
echo("Please clear your facebook cookie and re login. The state id we use to check the authenticity of the session does not match.");
}
$access_token = $params['access_token'];
此時,訪問令牌可用。
稍后在代碼中,我檢查$facebook->getUser()
返回用戶ID。 它不是。
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
相反,如果我使用$ facebook-> getLoginUrl(),並讓用戶點擊一次(fb檢測到用戶已登錄並且沒有彈出登錄框,而只是返回到app url),$ facebook->從那時起,getUser()確實返回id。
如果您需要更多信息,請告知我們。
函數_accessServer將另一個請求打開回服務器,發送訪問令牌。
函數_loginPopup應該打開Facebook登錄彈出窗口,請求用戶具有適當的權限以“允許訪問”您的應用程序。
然后,PHP應用程序應將訪問令牌傳遞回Facebook API
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.