Hi Guys I want to post on my facebook page by using Facebook PHP SDK v5. Here are my codes but after running, I get the following errors

*Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookAuthorizationException' with message '(#200) Insufficient permission to post to target on behalf of the viewer' in ................

My codes

<?php
    session_start();
    require_once("Facebook/autoload.php");        
    use Facebook\Facebook;
    use Facebook\FacebookRequest;
    use Facebook\GraphNodes\GraphNode;
    use Facebook\Helpers\FacebookRedirectLoginHelper;
    use Facebook\Exceptions\FacebookSDKException;
    use Facebook\Exceptions\FacebookResponseException;

    $api_id="8888090.......";
    $api_secret="6786bdcd97b18e94b9bbff4b2ecc723c";
    $url="http://localhost/feedreader/feedpost.php";

    $fb = new Facebook([
      "app_id" => $api_id,
      "app_secret" => $api_secret,
      "default_graph_version" => "v2.2",
      ]);

    $linkData = [
      'link' => 'http://www.daniweb.com',
      'message' => 'I Like DaniWeb IT Discussion Forum',
      ];

    $helper=$fb->getRedirectLoginHelper();    
    try{        
        $token=$helper->getAccessToken();        
        if(isset($token))
        {            
            $response = $fb->post('/357971644373038/feed', $linkData,$token);
            $graphNode = $response->getGraphNode();
            echo 'Posted with id: ' . $graphNode['id'];            
        }        
        else{
            $permision=array("scope"=>"email,publish_actions,manage_pages");
            echo "<a href='".$helper->getLoginUrl($url,$permision)."'>Click to post</a>";            
        }
    } 
    catch (Facebook\Exceptions\FacebookSDKException $e) 
    {
        echo $e->getMessage();
    }
?>

Can any one Help me on this? I need your help guys....

You have to add the appropriate permission in order to post to the timeline, this is done by appending the scope parameter to the login link, in your case the value to use should be publish_actions, but check the documentation to apply the correct permissions:

Read it carefully, especially the reference / public_actions section at the end of the document.

<?php
    session_start();
    require_once("Facebook/autoload.php");        
    use Facebook\Facebook;
    use Facebook\FacebookRequest;
    use Facebook\GraphNodes\GraphNode;
    use Facebook\Helpers\FacebookRedirectLoginHelper;
    use Facebook\Exceptions\FacebookSDKException;
    use Facebook\Exceptions\FacebookResponseException;
    $api_id="8888090.......";
    $api_secret="6786bdcd97b18e94b9bbff4b2ecc723c";
    $url="http://localhost/feedreader/feedpost.php";
    $fb = new Facebook([
      "app_id" => $api_id,
      "app_secret" => $api_secret,
      "default_graph_version" => "v2.2",
      ]);
    $linkData = [
      'link' => 'http://www.daniweb.com',
      'message' => 'I Like DaniWeb IT Discussion Forum',
      ];
    $helper=$fb->getRedirectLoginHelper();    
    try{        
        $token=$helper->getAccessToken();        
        if(isset($token))
        {            
            $response = $fb->post('/357971644373038/feed', $linkData,$token);
            $graphNode = $response->getGraphNode();
            echo 'Posted with id: ' . $graphNode['id'];            
        }        
        else{
            $permision=array("scope"=>"email,publish_actions,manage_pages");
            echo "<a href='".$helper->getLoginUrl($url,$permision)."'>Click to post</a>";            
        }
    } 
    catch (Facebook\Exceptions\FacebookSDKException $e) 
    {
        echo $e->getMessage();
    }
?>

You have to add the appropriate permission in order to post to the timeline, this is done by appending the scope parameter to the login link, in your case the value to use should be publish_actions, but check the documentation to apply the correct permissions:

https://developers.facebook.com/docs/facebook-login/permissions/v2.4
Read it carefully, especially the reference / public_actions section at the end of the document.

For the case of posting to personal profile timeline, the codes works right, but the problem is, if I want to post into a page just like company page

Ok, you have to fix two things here:

A) add publish_pages to the scope:

$permissions = ['publish_actions', 'manage_pages', 'email', 'publish_pages'];

B) the third argument for $fb->post() must be the page access token, not the user access token which is set previously. So:

$pageTokens = $fb->get('/me/accounts?fields=id,name,access_token')
                 ->getGraphEdge()
                 ->asArray();

foreach($pageTokens as $key => $value)
{
    # match page ID
    if($value['id'] == 357971644373038)
    {
        $pageToken = $value['access_token'];
        break;
    }

}

$response = $fb->post('/357971644373038/feed', $linkData, $pageToken);

The page access token expires after one hour. With these changes you should be able to write to a page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.