Search code examples
phpfacebookpublish

Graph returned an error: (#200) Permissions error


when I publish a post to my page, it's working fine, but when I post to page through set page ID instead of "me", the response as the following: Graph returned an error: (#200) Permissions error the source code as the following:

require_once 'src/Facebook/autoload.php';

//*
$config = array();
$config['appId'] = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_id'] = $config['appId'];
$config['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_secret'] = $config['secret'];
define('APP_ID', $config['appId']);
define('APP_SECRET', $config['secret']);
//$config['fileUpload'] = false; // optional
$config['default_graph_version'] = 'v2.5'; 
$config['page_id']  =   'xxxxxxxxxxxxxxxxxxxxxxxx';  

$config['facebook']['permissions'] = array(
  'email',
  'user_location',
  'user_birthday',
  'publish_actions',
  'publish_pages',
  'manage_pages',
  'public_profile',
);

$fb = new Facebook\Facebook($config);




if(isset($_SESSION['fb_access_token'])) {
    $accessToken = $_SESSION['fb_access_token'];

} else {

    $helper = $fb->getRedirectLoginHelper();
    try {
        $accessToken = $helper->getAccessToken();

        if(isset($accessToken)) {
            $oAuth2Client = $fb->getOAuth2Client();

            // longlived access token
            if (!$accessToken->isLongLived()) {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); 
            }
        }

    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}



if(isset($accessToken)) {
    // Logged in!
    $_SESSION['fb_access_token'] = (string) $accessToken;

    try {
        $response = $fb->get('/'.$config['page_id'].'/feed', $accessToken);

    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        //exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        //exit;
    } 
} else {
    $helper = $fb->getRedirectLoginHelper();

    $redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $loginUrl = $helper->getLoginUrl($redirect_url, [ 'manage_pages', 'publish_pages', 'publish_actions' ]);
    echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}





on other action in same page 


$page_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';







// instance
$fb = new Facebook\Facebook([
    'app_id'     => APP_ID,
    'app_secret' => APP_SECRET,
    'default_graph_version' => 'v2.4',
]);

$linkData = [
  'link' => encodeurl(array('page'=>'page','lang'=>'ar','id'=>$_id,'beg'=>0,'unuse'=>$xtitlex)),
  'message' => $xtitlex,
  'source' => $publish,//$fb->fileToUpload($publish),
  'caption' => DOMAIN_,
  "picture" => $publish,
  "name" => $xtitlex,
  "description" => $xdescriptionx
 ];



try {
    //
    $response = $fb->post('/'.$config['page_id'].'/feed', $linkData, $page_access_token);

} catch(Facebook\Exceptions\FacebookResponseException $e) {
    //print_r($linkData);
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}
?>

Solution

  • First check if the Access Token you used is generated as "Page Access Token" not "User Token". It is a bit tricky and took me a moment.

    1. go to "https://developers.facebook.com/tools/explorer/"
    2. drop down "User Token" menu
    3. pick "Page Access Token"
    4. blue button "Generate Access Token"
    5. copy and use new token

    Every time you add or remove Permissions, token changes!

    Screenshot screenshot of permissions