60

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);
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
user401183
  • 2,530
  • 4
  • 27
  • 25

7 Answers7

32

Use:

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

instead of

$facebook->api('/me')

it works.

Community
  • 1
  • 1
yacon
  • 1,112
  • 1
  • 10
  • 17
  • 3
    Its not a bug; as the first comment on the accepted answer says ... 'me' looks for a valid facebook session whereas 'facebook user_id' does not – Mike Jan 07 '13 at 09:31
32

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.

ifaour
  • 38,035
  • 12
  • 72
  • 79
  • 2
    +1 ... thanks for the answer.. was breaking my head on this... 'me' looks for a valid facebook session... whereas 'facebook user_id' does not.. just what I wanted – Ninja May 23 '12 at 11:36
  • here we are checking for the permissions for the app?.First time it shown the facebook dialog for goto App.i have clicked it.After that now when i opens the page it shows blank page – V I J E S H Aug 02 '12 at 09:43
  • @VIJESH, are you using the code above as is? you are aware that this is incomplete code just to get you started. – ifaour Sep 28 '12 at 16:35
  • thankyou I have resolved my problem 50% right now please if you can help I have a similar topic here http://stackoverflow.com/questions/43632458/php-facebook-sdk-doesnt-create-the-user-in-wordpress :( –  May 01 '17 at 14:53
18

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 ...)
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
Arjay Waran
  • 433
  • 5
  • 9
  • 1
    I love you for this answer! This saved me regarding an issue I had with the new Dec 5th breaking-changes. I was researching on this for days. – hugo der hungrige Nov 30 '12 at 00:55
  • 1
    dear god, just spent 2 hours looking for this and I too forgot to set my saved token. bleh. – Ryan Mar 22 '13 at 18:40
4

After a certain amount of time, your access token expires.

To prevent this, you can request the 'offline_access' permission during the authentication, as noted here: Do Facebook Oauth 2.0 Access Tokens Expire?

Community
  • 1
  • 1
Jeroen
  • 13,056
  • 4
  • 42
  • 63
0

Had the same problem and the solution was to reauthorize the user. Check it here:

<?php

 require_once("src/facebook.php");

  $config = array(
      'appId' => '1424980371051918',
      'secret' => '2ed5c1260daa4c44673ba6fbc348c67d',
      'fileUpload' => false // optional
  );

  $facebook = new Facebook($config);

//Authorizing app:

?>
<a href="<?php echo $facebook->getLoginUrl(); ?>">Login con fb</a>

Saved project and opened on my test enviroment and it worked again. As I did, you can comment your previous code and try.

Daniel
  • 1
  • 3
0

I have got the same issue when tried to get users information without auth.

Check if you have loggen in before any request.

 $uid = $facebook->getUser();

 if ($uid){
      $me = $facebook->api('/me');
 }

The code above should solve your issue.

Vlad
  • 3,465
  • 1
  • 31
  • 24
0

I had the same issue but I can solve this issue by cleaning the cookies and cache from my browser (ctrl+shift+R). Also, you must ensure that your access token is active.

Hope this will help you to solve your problem.

Faisal
  • 4,591
  • 3
  • 40
  • 49