0

I'm new to the 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. 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:

I tried uncommenting this line in bootstrap.php but it did not work 'auth' => MODPATH.'auth', // Basic authentication

EDIT 2: When I run the following code

$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

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()
2
  • 1
    Is your app configured as a Native/Desktop app? That might cause this issue. Sep 11, 2013 at 16:19
  • +1 Thank you for your reply. How do I check whether my app is configured as a Native app? I searched the bootstrap.php file but did not see any such setting.
    – Anthony
    Sep 11, 2013 at 16:24

2 Answers 2

2

From the Facebook Developer documentation:

Note: Applications that are configured as Native/Desktop apps will not be able to make API calls that require an application access_token.

Go to Settings → Advanced, and change it to Web:

That's the most common cause for this issue and changing it to Web should solve it.


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. A work-around source is to imitate $_REQUEST used in base_facebook.php with $_GET, $_POST and $_COOKIE super-global arrays.

Find the following code in your 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.

9
  • +1 Thank you for your guidance to the advanced settings. It's already set to web though. However, I noticed the message: This app is in sandbox mode (only visible to Admins, Developers and Testers).
    – Anthony
    Sep 11, 2013 at 16:41
  • 1
    @Anthony: What is the PHP version / PHP SDK version? Sep 11, 2013 at 16:43
  • +1 My PHP version is 5.4.16. However, I'm still trying to find the version number for Facebook SDK for PHP. I downloaded it only yesterday... how do I get this version number?
    – Anthony
    Sep 11, 2013 at 16:56
  • 1
    @Anthony: Sorry, it was a typo. I meant base_facebook.php. Sep 11, 2013 at 20:34
  • 1
    @Anthony: yes, change all the occurrences. Sep 12, 2013 at 12:52
0

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.