Php – Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user

apifacebookfacebook-graph-apiPHP

I have a Facebook app that does not seem to work no matter what I have tried.
I get the error:

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in ……./src/base_facebook.php on line 1024

This is the code:

<?php
session_start();
include 'config.php';
require_once 'src/facebook.php';
$facebook = new Facebook(array(
        'appId' => $appid,
        'secret' => $appsecret,
        'cookie' => true
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');

$coded = $_REQUEST['code'];

$access_token = $facebook->getAccessToken();
$name = "".$user_profile['name']."";
$fbid = "".$user_profile['id']."";

function RandomLine($filename) { 
    $lines = file($filename) ; 
    return $lines[array_rand($lines)] ; 
} 
$reason = RandomLine("reason.txt");   

function spin($content)
{   
    $pattern = '/\{\{([^{}]*)\}\}/si';
    preg_match_all($pattern,$content,$matches);
    for ($i=0; $i< count($matches[0]); $i++) {
        $search = explode("|",$matches[1][$i]);
        shuffle($search);
        $content = str_replace($matches[0][$i],$search[0],$content);
    }
    return $content;
}

$links = '{{http://google.com}}'; 

$messages = '{{test}}'; 

$canvas = imagecreatefromjpeg ("bg.jpg");                                   // background image file
$black = imagecolorallocate( $canvas, 0, 0, 0 );                         // The second colour - to be used for the text
$font = "arial.ttf";                                                         // Path to the font you are going to use
$fontsize = 20;                                                             // font size

$birthday = "".$user_profile['birthday']."";
$death = "- ".date('d/m/Y', strtotime( '+'.rand(0, 10000).' days'))."";

imagejpeg( $canvas, "tmp/".$fbid.".jpg", 50 );

$facebook->setFileUploadSupport(true);

$album_details = array(
        'message'=> 'test',
        'name'=> 'test'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);

$album_uid = $create_album['id'];

$args = array('message' => ''.spin($messages).' '.spin($links).'');
                $args['image'] = '@' . realpath('tmp/'.$fbid.'.jpg');
                $data = $facebook->api('/'.$album_uid.'/photos', 'post', $args);
                unlink('tmp/'.$fbid.'.jpg');



ImageDestroy( $canvas );
header("http://google.com");

?>

I have tried everything. I have been googling for the answer for ages.
Please help.

Best Answer

It seems to me like the authentication part is missing. I have not used the facebook API, but OAuth requires a roundtrip.

You must first get a request token and get the user to authenticate it, usually via a redirection to the service. When he comes back, the access token can be obtained.

You provided too much code here. Make sure the authentication works first.

Maybe you just need to move the getAccessToken() call before the getUser() one.