[英]facebook iframe app; php sdk getUser() returns valid id on page one but not for any other page
我有一個Facebook iframe應用程序,該應用程序可以正確登錄並授權該應用程序,但getUser()僅在第一頁上有效。 用戶單擊iframe中指向新頁面的鏈接后,getUser()就會返回0。
奇怪的是,相同的代碼可用於另一個應用程序...我要進行所有單擊,而getUser()返回有效的ID。
無法使用的應用程序: https : //apps.facebook.com/celestial_glory/
可以做到的(相同的代碼庫): https : //apps.facebook.com/uprisingstlouis/
這是我正在使用的代碼:
require_once ('fb/facebook.php');
// snip... set $app_id, $secret, and $canvas_page
// first, try normal facebook getUser(). If that works, awesome.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
));
$signed_request = $_REQUEST['signed_request'];
// Get User ID
$user = $facebook->getUser();
if ($user != '0') return 'fb=' . $user; // works once
// getUser() didn't work. Try oAuth. Maybe user needs to log in or
// authorize the game?
$auth_url = 'http://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($canvas_page);
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo '<a target="_top" href="' . $auth_url . '">Login to Facebook</a>';
exit;
// normally we would auto-redirect, but with a uid of 0, this just auto-redirects
// echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
return 'fb=' . $data['user_id'];
}
有任何想法嗎? 我對應用程序ID和機密以及畫布頁面進行了三重檢查。 如果這些都不對,我希望沒有頁面,甚至沒有頁面都可以。
將Facebook PHP-SDK初始化更改為:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true // this!
));
getUser
在第一頁上工作,因為它可以從signed_request
(Facebook發布到您的畫布頁面URL)中吸引用戶。 因此,一旦用戶開始在您的應用程序中進行更深入的導航,您就需要一種跟蹤用戶的方法。 您可以自己通過某種方式傳遞signed_request
也可以按照上面的建議直接啟用內置的PHP-SDK cookie支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.