简体   繁体   English

没有Facebook用户ID返回。 为什么?

[英]No Facebook User ID returned. Why?

I'm new to the Facebook SDK for PHP . 我是Facebook SDK for PHP的新手。 I have a really basic question here ( this answer was not helpful ): I am currently logged into my Facebook profile, however, getUser() returns nothing. 我在这里有一个非常基本的问题( 这个答案没有帮助 ):我目前登录到我的Facebook个人资料,但是, getUser()没有返回任何内容。 Why? 为什么?

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;
            // ...

EDIT 1: 编辑1:

I tried uncommenting this line in bootstrap.php but it did not work 'auth' => MODPATH.'auth', // Basic authentication 我尝试在bootstrap.php中取消注释这一行,但它没有工作'auth' => MODPATH.'auth', // Basic authentication

EDIT 2: When I run the following code 编辑2:当我运行以下代码时

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

I get this error 我收到这个错误

An active access token must be used to query information about the current user. 必须使用活动访问令牌来查询有关当前用户的信息。

EDIT 3 编辑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()

From the Facebook Developer documentation : 来自Facebook Developer文档

Note: Applications that are configured as Native/Desktop apps will not be able to make API calls that require an application access_token. 注意:配置为Native / Desktop应用程序的应用程序将无法进行需要应用程序access_token的API调用。

Go to Settings → Advanced, and change it to Web : 转到设置→高级,然后将其更改为Web

That's the most common cause for this issue and changing it to Web should solve it. 这是导致此问题的最常见原因,将其更改为Web应解决此问题。


If that wasn't the case, it's probably an issue with PHP SDK, which relies on $_REQUEST super-global array from the server which has changed in PHP 5.4.x because of default php.ini settings having been changed. 如果不是这样的话,那可能是PHP SDK的一个问题,它依赖于服务器中的$_REQUEST超全局数组,由于默认的php.ini设置已被更改,因此已在PHP 5.4.x中进行了更改。 A work-around source is to imitate $_REQUEST used in base_facebook.php with $_GET , $_POST and $_COOKIE super-global arrays. 一个解决source是模仿$_REQUEST中使用base_facebook.php$_GET$_POST$_COOKIE超全局数组。

Find the following code in your base_facebook.php : 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;
}

?>

Change it to: 将其更改为:

<?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. 看到不同。

I was stuck up with similar issue, here is the solution that may help you. 我遇到了类似的问题,这是可以帮助你的解决方案。

 $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());

}

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

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