【问题标题】:No Facebook User ID returned. Why?未返回 Facebook 用户 ID。为什么?
【发布时间】:2013-09-11 16:09:21
【问题描述】:

我是Facebook SDK for PHP 的新手。我在这里有一个非常基本的问题 (this answer was not helpful):我目前登录到我的 Facebook 个人资料,但是,getUser() 没有返回任何内容。为什么?

require_once("vendor/facebook-php-sdk/src/facebook.php");

class Controller_Test extends Controller {
    public function action_me()
        {
            $config = array();
            $config['appId'] = 'dfjq454...';
            $config['secret'] = 'adfw424...';
            $config['fileUpload'] = false;
            $facebook = new Facebook($config);  
            $user = $facebook->getUser();

            // Returns '0'
            echo "Me: ".$user;
            // ...

编辑 1:

我尝试在 bootstrap.php 中取消注释这一行,但它不起作用'auth' => MODPATH.'auth', // Basic authentication

编辑 2: 当我运行以下代码时

$user_profile = $facebook->api('/me');
        echo "My Profile: ".$user_profile;

我收到此错误

必须使用活动访问令牌来查询有关 当前用户。

编辑 3

FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
DOCROOT\vendor\facebook-php-sdk\src\base_facebook.php [ 1272 ]
1267    *
1268    * @param $result array A record storing the error message returned
1269    *                      by a failed API call.
1270    */
1271   protected function throwAPIException($result) {
1272     $e = new FacebookApiException($result);
1273     switch ($e->getType()) {
1274       // OAuth 2.0 Draft 00 style
1275       case 'OAuthException':
1276         // OAuth 2.0 Draft 10 style
1277       case 'invalid_token':
DOCROOT\vendor\facebook-php-sdk\src\base_facebook.php [ 881 ] » BaseFacebook->throwAPIException(arguments)
{PHP internal call} » BaseFacebook->_graph(arguments)
DOCROOT\vendor\facebook-php-sdk\src\base_facebook.php [ 654 ] » call_user_func_array(arguments)
APPPATH\classes\Controller\test.php [ 17 ] » BaseFacebook->api(arguments)
SYSPATH\classes\Kohana\Controller.php [ 84 ] » Controller_Test->action_me()
{PHP internal call} » Kohana_Controller->execute()
SYSPATH\classes\Kohana\Request\Client\Internal.php [ 97 ] » ReflectionMethod->invoke(arguments)
SYSPATH\classes\Kohana\Request\Client.php [ 114 ] » Kohana_Request_Client_Internal->execute_request(arguments)
SYSPATH\classes\Kohana\Request.php [ 986 ] » Kohana_Request_Client->execute(arguments)
DOCROOT\index.php [ 118 ] » Kohana_Request->execute()

【问题讨论】:

  • 您的应用是否配置为原生/桌面应用?这可能会导致此问题。
  • +1 感谢您的回复。如何检查我的应用是否配置为原生应用?我搜索了 bootstrap.php 文件,但没有看到任何此类设置。

标签: php facebook kohana kohana-3


【解决方案1】:

来自Facebook Developer documentation:

注意:配置为 Native/Desktop 应用程序的应用程序将无法进行需要应用程序 access_token 的 API 调用。

转到设置→高级,并将其更改为Web:

这是导致此问题的最常见原因,将其更改为 Web 应该可以解决问题。


如果不是这种情况,则可能是 PHP SDK 的问题,它依赖于来自服务器的 $_REQUEST 超级全局数组,由于默认的 php.ini 设置已更改,因此在 PHP 5.4.x 中该数组已更改. source 的解决方法是用$_GET、$_POST 和$_COOKIE 超全局数组来模仿base_facebook.php 中使用的$_REQUEST。

在您的base_facebook.php 中找到以下代码:

<?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;
}

?>

改成:

<?php

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

?>

See the difference.

【讨论】:

  • +1 感谢您对高级设置的指导。虽然它已经设置为网络。但是,我注意到了这条消息:This app is in sandbox mode (only visible to Admins, Developers and Testers).
  • @Anthony:PHP 版本/PHP SDK 版本是多少?
  • +1 我的 PHP 版本是 5.4.16。但是,我仍在尝试查找 Facebook SDK for PHP 的版本号。我昨天才下载的...我如何获得这个版本号?
  • @Anthony:抱歉,打错了。我的意思是base_facebook.php。
  • @Anthony:是的,改变所有的出现。
【解决方案2】:

我遇到了类似的问题,这里有一个可以帮助你的解决方案。

 $facebook = new Facebook(array(
    'appId'  => 'xxxx',
    'secret' => 'xxxxx',
    "redirect_uri" => "set the uri to point to the same page"
 ));

$user = $facebook->getUser();

if($user)
{
//your task
} else {
header('location:'. $facebook->getLoginUrl());

}

$user = $facebook->getUser();

if($user)
{
 //your task
} else {
header('location:'. $facebook->getLoginUrl());

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多