7

I am trying to update Facebook user status using Graph api. My code is

<?php 
    require 'facebook.php'; 
    $facebook = new Facebook(array( 
    'appId' =>'389694921095423',
    'secret' =>'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'cookie' => true
    )); 
    $access_token = $facebook->getAccessToken();
    echo($access_token);
    $me = null; 

    try 
    { 
        $uid = $facebook->getUser(); 
        $me = $facebook->api('/me'); 
        echo "Welcome User: " . $me['name'] . "<br />"; 
        //access permission
        $permissions_needed = array('publish_stream', 'read_stream', 'offline_access', 'manage_pages');
        foreach($permissions_needed as $perm) 
        {  
            if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 )
            {    
            $login_url_params = array(
                'scope' => 'publish_stream,read_stream,offline_access,manage_pages',         
                'fbconnect' =>  1,         
                'display'   =>  "page",         
                'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']      
                );    
            $login_url = $facebook->getLoginUrl($login_url_params);   
            header("Location: {$login_url}");
            exit(); 
            }
        }
        //Access permission

        $post_id = $facebook->api("/$uid/feed", "post", array("message"=>"Hello World!")); 
        if(isset($post_id)) 
        {
            echo "A new post to your wall has been posted with id: $post_id"; 
        }
    } 
    catch (FacebookApiException $e) 
    { 
        echo($e); 
    } 
?>

The problem is that it shows a run time error like Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. I have generated the Access Tocken. Where i have to use that to remove this exception Thanks in advance

2
  • “I have generated the Access Tocken.” – where and when did you create it? Sounds like you’re using an already expired token. You can use the debug tool to check if your token is still valid or not: developers.facebook.com/tools/debug
    – CBroe
    Aug 2, 2012 at 11:51
  • 1
    That is because $facebook->getUser() is return 0 , and the user is not login. May 7, 2013 at 15:06

2 Answers 2

32

Try changing the line:

$me = $facebook->api('/me');

to

$me = $facebook->api('/'.$uid); 

Also try upgrading your sdk if you are not using the latest version and if you just upgraded the sdk and it caused the trouble, than you can try to downgrade the version and check.

4
  • 6
    well, this is facebook! bizare errors, and the biggest problem is that this error can be caused by a lot of others problems too. Feb 17, 2013 at 14:43
  • Thanks a lot, this worked for me! But still can't figure why is "/me" still standing on FB documentation? Nov 14, 2013 at 14:58
  • So this is still valid? Because sometimes I get the error about the token and still cannot be sure. Why is the fb docs still using the me and what is the difference?
    – Jimmy Kane
    Dec 5, 2013 at 9:39
  • Well, /me is a special symbol used by facebook, to identify the current logged in user, it is similar to using the current logged in user id. You should actually try to debug the response that you get from facebook, that will help you locate the trouble more accurately. Dec 5, 2013 at 13:44
5

Clear your cache & cookies (if you are testing or developing application) and Please do as follow:-

try
{  
  $fb_user_info = $facebook->api('/me');
} catch (Exception $ex)
{
  $facebook->destroySession();
  $params = array(
      'scope' => 'YOUR_PERMISSIONS',
      'redirect_uri' => 'YOUR_SITE_URL'
  );
  $fb_login_url = $facebook->getLoginUrl($params);  
  header("Location:" . $fb_login_url);
}
3
  • sidenote: remember to call exit; after header('Location: $url');
    – Raptor
    Oct 2, 2013 at 5:03
  • 1
    typo error in your side note: it should be header('Location: '. $url); we cannot use variable in single quotes or it should be in double quotes Oct 2, 2013 at 12:49
  • Yes. It's a stupid typo. Thanks for reminder. ( can't edit though )
    – Raptor
    Oct 3, 2013 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.