2
因此,我使用Facebook注册插件以及Javascript SDK(使用xfbml显示)。注册工作正常,我的代码处理它看起来像这样:使用Facebook注册插件后无法获取用户
# Check if the form has been submitted.
if(array_key_exists('signed_request', $_REQUEST))
{
define('FACEBOOK_APP_ID', 'REMOVED');
define('FACEBOOK_SECRET', 'REMOVED');
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST) {
# Load the Users model.
$this->load->model('Users');
# Save the data from the form.
$response = parse_signed_request($_REQUEST['signed_request'], FACEBOOK_SECRET);
# Save the user's email.
$user['email'] = $response['registration']['email'];
# Boolean that stores if the user registered through FB.
$facebook;
// the rest is irrelevant
所以一旦用户注册我重定向他们回到主页。当我登录我的Facebook帐户时,我的网站显示在我使用的应用程序下(隐私设置中)。
尽管如此,当我尝试在主页上使用PHP SDK时,用户仍未出现身份验证。以下代码将$ user_id和“无效”作为$无效打印出“0”。以下代码适用于主页:
require 'application/sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => 'REMOVED',
'secret' => 'REMOVED',
));
// Get User ID
$user = $facebook->getUser();
echo $user;
$isvalid;
try {
$facebook->api('/me');
$isvalid = true;
} catch (FacebookApiException $e) {
$isvalid = false;
echo "not valid";
}
我在做什么错?
谢谢!