Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook Uncaught OAuthException: An active access token must be used to query information about the current user

I've been struggling to find out what is happening with this. My scripts were working fine for a bit and suddenly half stopped.

I'm accessing the api and am getting back an access token. With the access token, I can access a users public info just fine. However, when I try to post info to their FB account I get this error.

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

Any idea what is happening here? I'm also using sessions on my site to keep track of internal user ids. Not sure if my sessions could be causing a problem.

This is my upload script where I'm getting an error.

require 'facebook/src/facebook.php';   // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array(   'appId'  => '12345678',   'secret' => 'REMOVED',   'fileUpload' => true,    'cookie' => true, )); $facebook->setFileUploadSupport(true);   $me = null; // Session based API call. if ($session) {   try {     $uid = $facebook->getUser();     $me = $facebook->api('/me');   } catch (FacebookApiException $e) {     error_log($e);   } }   // login or logout url will be needed depending on current user state. if ($me) {   $logoutUrl = $facebook->getLogoutUrl(); } else {   $loginUrl = $facebook->getLoginUrl(); }   $photo_details = array('message' => 'my place'); $file='photos/my.jpg'; //Example image file $photo_details['image'] = '@' . realpath($file); $upload_photo = $facebook->api('/me/photos', 'post', $photo_details); 
like image 517
user401183 Avatar asked May 17 '11 17:05

user401183


People also ask

What is a Facebook access token?

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.

Does Facebook access token expire?

When your app uses Facebook Login to authenticate someone, it receives a User access token. If your app uses one of the Facebook SDKs, this token lasts for about 60 days. However, the SDKs automatically refresh the token whenever the person uses your app, so the tokens expire 60 days after last use.


2 Answers

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.

like image 196
ifaour Avatar answered Sep 26 '22 18:09

ifaour


Use:

$facebook->api('/'.$facebook_uid) 

instead of

$facebook->api('/me') 

it works.

like image 32
yacon Avatar answered Sep 24 '22 18:09

yacon