为什么Facebook PHP SDK getUser总是返回0?

fd8*_*8s0 39 php facebook facebook-graph-api

我正在尝试使用一个需要Facebook用户提供信息的网站,我正在使用PHP和JS SDK.

我有一个PHP函数:

public function isLoggedOnFacebook() {
    $user = $this->_facebook->getUser();
    if ($user) {
        return $this->_facebook->api("/$user");
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

在一个持有SDK中的facebook对象的类中$this->_facebook.

然后在块上我这样做:

<?php if (!$this->isLoggedOnFacebook()): ?>
<div>
   <fb:login-button show-faces="true" perms="email" width="500" />
</div>
<?php endif ?>
Run Code Online (Sandbox Code Playgroud)

并且FB JS环境已正确设置(我认为),因此它可以工作.因此,用户可以弹出并授权该站点.

问题是即使在用户授权应用程序后,$ user始终为0,意味着$facebook->getUser()始终返回0,然后列出用户的面孔,包括已登录的用户,但如果我拨打电话$facebook->api('/me')或其他什么,那么它将会抛出无效的令牌异常.

我已经看到了这个问题,但我还没有看到解决方案,我不知道问题出在哪里,我的想法已经用完了.

在应用程序部分的开发人员的Facebook页面上有一个网站选项卡,您可以在其中设置您的站点URL和站点域,我认为这是我的问题的原因,但我不知道究竟是什么字段应该包含.

Nor*_*oan 23

我有同样的问题,我想通了,这是因为SDK使用变量$_REQUEST,并在我的环境是不是合并与真正的$_GET,$_POST$_COOKIE变量.

我认为这取决于PHP版本,这就是为什么有人通过启用cookie使其工作.

我在base_facebook.php中找到了这段代码:

protected function getCode() {
    if (isset($_REQUEST['code'])) {
        if ($this->state !== null &&
                isset($_REQUEST['state']) &&
                $this->state === $_REQUEST['state']) {

            // CSRF state has done its job, so clear it
            $this->state = null;
            $this->clearPersistentData('state');
            return $_REQUEST['code'];
        } else {
            self::errorLog('CSRF state token does not match one provided.');
            return false;
        }
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

我通过创建$ server_info变量来修改它,如下所示.

protected function getCode() {
    $server_info = array_merge($_GET, $_POST, $_COOKIE);

    if (isset($server_info['code'])) {
        if ($this->state !== null &&
                isset($server_info['state']) &&
                $this->state === $server_info['state']) {

            // CSRF state has done its job, so clear it
            $this->state = null;
            $this->clearPersistentData('state');
            return $server_info['code'];
        } else {
            self::errorLog('CSRF state token does not match one provided.');
            return false;
        }
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我也有用.讨厌更改库,但它可以工作.这是在codeigniter安装,如果这有助于任何人. (3认同)

anu*_*jkk 5

我遇到了类似的问题.$ facebook-> getUser()返回0并且有时当用户实际未登录时它返回有效的用户ID,当我尝试进行图形api调用时导致Fatal Oauth错误.我终于解决了这个问题.我不知道它是否是正确的方式,但它的工作原理.这是代码:

<?php
include 'includes/php/facebook.php';
$app_id = "APP_ID";
$app_secret = "SECRET_KEY";
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
    'cookie' => true
));

$user = $facebook->getUser();

if ($user <> '0' && $user <> '') { /*if valid user id i.e. neither 0 nor blank nor null*/
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) { /*sometimes it shows user id even if user in not logged in and it results in Oauth exception. In this case we will set it back to 0.*/
error_log($e);
$user = '0';
}
}
if ($user <> '0' && $user <> '') { /*So now we will have a valid user id with a valid oauth access token and so the code will work fine.*/
echo "UserId : " . $user;

$params = array( 'next' => 'http://www.anujkumar.com' );
echo "<p><a href='". $facebook->getLogoutUrl($params) . "'>Logout</a>";

$user_profile = $facebook->api('/me');
echo "<p>Name : " . $user_profile['name'];
echo "<p>";
print_r($user_profile);

} else {/*If user id isn't present just redirect it to login url*/
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'email,offline_access'))}");
}

?>
Run Code Online (Sandbox Code Playgroud)


fd8*_*8s0 0

我的具体问题的答案是我使用的 JS SDK 和 PHP SDK 版本之间存在不兼容,只需升级它们即可解决。

当这个问题是由各种不同的原因引起时,其症状是相似的,因此您可以很好地通过本页中提供的不同答案进行搜索。