facebook Uncaught OAuthException: An active access token must be used to query information about the current user
Just check for the current Facebook user id $user
and if it returned null then you need to reauthorize the user (or use the custom $_SESSION
user id value - not recommended)
require 'facebook/src/facebook.php';// Create our Application instance (replace this with your appId and secret).$facebook = new Facebook(array( 'appId' => 'APP_ID', 'secret' => 'APP_SECRET',));$user = $facebook->getUser();$photo_details = array('message' => 'my place');$file='photos/my.jpg'; //Example image file$photo_details['image'] = '@' . realpath($file);if ($user) { try { // We have a valid FB session, so we can use 'me' $upload_photo = $facebook->api('/me/photos', 'post', $photo_details); } catch (FacebookApiException $e) { error_log($e); }}// login or logout url will be needed depending on current user state.if ($user) { $logoutUrl = $facebook->getLogoutUrl();} else {// redirect to Facebook login to get a fresh user access_token $loginUrl = $facebook->getLoginUrl(); header('Location: ' . $loginUrl);}
I've written a tutorial on how to upload a picture to the user's wall.
Use:
$facebook->api('/'.$facebook_uid)
instead of
$facebook->api('/me')
it works.
So I had the same issue, but it was because I was saving the access token but not using it. It could be because I'm super sleepy because of due dates, or maybe I just didn't think about it! But in case anyone else is in the same situation:
When I log in the user I save the access token:
$facebook = new Facebook(array( 'appId' => <insert the app id you get from facebook here>, 'secret' => <insert the app secret you get from facebook here>));$accessToken = $facebook->getAccessToken();//save the access token for later
Now when I make requests to facebook I just do something like this:
$facebook = new Facebook(array( 'appId' => <insert the app id you get from facebook here>, 'secret' => <insert the app secret you get from facebook here>));$facebook->setAccessToken($accessToken);$facebook->api(... insert own code here ...)