简体   繁体   English

Facebook iframe应用; php sdk getUser()返回第一页上的有效ID,但不适用于其他任何页面

[英]facebook iframe app; php sdk getUser() returns valid id on page one but not for any other page

I have a facebook iframe app which correctly logs in and authorizes the app, but getUser() only works on the first page. 我有一个Facebook iframe应用程序,该应用程序可以正确登录并授权该应用程序,但getUser()仅在第一页上有效。 As soon as a user clicks a link to a new page within the iframe, getUser() returns 0. 用户单击iframe中指向新页面的链接后,getUser()就会返回0。

What's strange is that this same code works for another app... I do all the clicking I want and getUser() returns a valid ID. 奇怪的是,相同的代码可用于另一个应用程序...我要进行所有单击,而getUser()返回有效的ID。

The app that doesn't work: https://apps.facebook.com/celestial_glory/ 无法使用的应用程序: https : //apps.facebook.com/celestial_glory/

The one that does (same codebase): https://apps.facebook.com/uprisingstlouis/ 可以做到的(相同的代码库): https : //apps.facebook.com/uprisingstlouis/

Here's the code I am using: 这是我正在使用的代码:

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'];
}

any ideas? 有任何想法吗? I have triple-checked app ids and secrets and canvas pages. 我对应用程序ID和机密以及画布页面进行了三重检查。 If those were wrong, I expect no page, not even the first, would work. 如果这些都不对,我希望没有页面,甚至没有页面都可以。

Change Facebook PHP-SDK initialization to: 将Facebook PHP-SDK初始化更改为:

$facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' => $secret,
  'cookie' => true // this!
));

getUser works on the first page because it can get the user from signed_request (POST'ed by Facebook to your canvas page URL). getUser在第一页上工作,因为它可以从signed_request (Facebook发布到您的画布页面URL)中吸引用户。 Thus you need some way to track your user once he starts navigation deeper within your application. 因此,一旦用户开始在您的应用程序中进行更深入的导航,您就需要一种跟踪用户的方法。 You could pass signed_request somehow all by yourself or simply enable built-in PHP-SDK cookie support as suggested above. 您可以自己通过某种方式传递signed_request也可以按照上面的建议直接启用内置的PHP-SDK cookie支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM