0

I am simply trying to upload an image to the user's timeline using PHP. After my app is installed I am using the following code but getting the error: OAuthException: An active access token must be used to query information about the current user.

I am able to echo an access token via the $token variable so I know its there. I've looked everywhere and tried to fix it but no success. Heres the code...

<?php  

include_once "src/facebook.php";  
$app_id = 'xxxxxx';  
$application_secret = 'xxxxxx';  

$facebook = new Facebook(array(  
'appId'  => $app_id,  
'secret' => $application_secret,  
'cookie' => true, // enable optional cookie support  
));  
if ($facebook->getUser())
{
  $user = $facebook->getUser();
  //die($token);

        try
        {
            $token = $facebook->getAccessToken();
            $facebook->setFileUploadSupport(true);
            $file = "5star.png";
            $post_data = array(
            "message" => "My photo caption",
            "source" => '@' . realpath($file)
            );
            //die("/me/photos/?access_token=$token");
            $data['photo'] = $facebook->api("/me/photos?access_token=$token", 'post', $post_data);
        } catch (FacebookApiException $e)
        {
            die($e);
        }

header( 'Location: http://google.com' ) ;
}else
{
  $loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=$app_id&redirect_uri=http://apps.facebook.com/yourprofilerating/&scope=user_photos,email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown";  
  echo "<script> top.location.href='" . $loginUrl . "'</script>";
}

?>
xendi
  • 2,332
  • 5
  • 40
  • 64
  • http://stackoverflow.com/questions/6034813/facebook-uncaught-oauthexception-an-active-access-token-must-be-used-to-query-i – Ja͢ck May 12 '12 at 07:16

1 Answers1

1

You may have an invalid or expired access token, check out Handling Invalid and Expired Access Tokens. Insert the code provided in this article before the api call and after the declaration of $token.

codeymcgoo
  • 565
  • 4
  • 8
  • I don't think that is the issue. Isn't the access token valid as long as I'm logged into facebook and accessing the app? im installing the app and running this code immediately after. – xendi May 12 '12 at 09:48
  • If you look at the page you linked me to you will see that the error I am experiencing is not one of the errors listed. – xendi May 12 '12 at 19:22
  • In the Extended Permissions section of the Auth Dialog for your app, do you require the publish_stream permission? – codeymcgoo May 15 '12 at 22:19