2

Hi I need to post message on facebook fan page

this is my code When I run this code I have following error (#200) The user hasn't authorized the application to perform this action if access_token token problem ? how can i create access_token

<?php
    require 'facebook-php-sdk-master//src/facebook.php';

    $appId = '1617691071796143';
    $secret = 'd84420ccfe2fa7eecac50ca96936bb21';
    $returnurl = 'lankabird.com';
    $permissions = 'manage_pages, publish_stream, offline_access,read_stream';

    $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
    $fbuser = $fb->getUser();

    if($fbuser){

        $page_id = "1596941017191630";
        $page_access_token = "";
        //$page_access_token = "1617691071796143|d84420ccfe2fa7eecac50ca96936bb21";
        $result =  $fb->api("/me/accounts");

        // loop trough all your pages and find the right one
        if( !empty($result['data']) )
        {
           foreach($result["data"] as $page) 
           {
             if($page["id"] == $page_id)
             {
               $page_access_token = $page["access_token"];
               break;
             }
           }
        }
        else
        {
          echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
        }

        $fb->setAccessToken($page_access_token);

        // Now try to post on page's wall
        try{
            $message = array(
                'message' => "YOUR MESSAGE",
            );

            $result = $fb->api('/'.$page_id.'/feed','POST',$message);
            if($result)
            {
                echo 'Successfully posted to Facebook Wall...';
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }

    }else{
        $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
        echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
    }
?>
Komal12
  • 3,340
  • 4
  • 16
  • 25
channasmcs
  • 1,104
  • 12
  • 27

2 Answers2

11

publish_stream and offline_access are deprecated since years, where do you guys keep copying your code? You need publish_actions (and of course manage_pages in your case) to post on the wall, as you can read in the docs: https://developers.facebook.com/docs/graph-api/reference/v2.2/page/feed#publish

Also, keep in mind that you need to go through a review process with those permissions if you want to make your App public: https://developers.facebook.com/docs/apps/review/login

About the read_stream permission:

This permission is granted to apps building a Facebook-branded client on platforms where Facebook is not already available. For example, Android and iOS apps will not be approved for this permission. In addition, Web, Desktop, in-car and TV apps will not be granted this permission.

Source: https://developers.facebook.com/docs/facebook-login/permissions/v2.2#reference-read_stream

...meaning, you will not get read_stream approved.

Edit: You can also use the permission publish_pages now, if you want to publish something "as Page". See the changelog for more information: https://developers.facebook.com/docs/apps/changelog#v2_3_changes

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • thank i usede http://stackoverflow.com/questions/18750786/facebook-php-api-post-to-wall-falls-under-recent-posts-by-others . after add read_stream i got verify the page error AN ERROR OCCURED: could not get the access_token. Please verify the page ID .. what is this AN ERROR OCCURED: could not get the access_token. Please verify the page ID – channasmcs Jan 29 '15 at 12:39
  • what for you do need read_stream? that permission is for reading existing posts of a user profile and completely irrelevant for pages. – andyrandy Jan 29 '15 at 12:47
  • also, you got 2 slashes in the require statement, and you are using the old php sdk. i suggest using the latest one. – andyrandy Jan 29 '15 at 12:48
  • btw, that error message is your own one. debug $result and make sure the user is authorized correctly. – andyrandy Jan 29 '15 at 12:51
  • why wasn't this answer accepted? The edit part is spot on, exactly what i needed. So in order to post to pages you only need the `manage_pages` and `publish_pages` permissions running with the latest php sdk from git. – Highstrike Jun 08 '15 at 10:12
0

You're using the wrong permissions. The ones you need are publish_actions and manage_pages.

$permissions = 'manage_pages,publish_actions';

See

Tobi
  • 31,405
  • 8
  • 58
  • 90
  • i have to face this after add $permissions = 'manage_pages,publish_actions'; AN ERROR OCCURED: could not get the access_token. Please verify the page ID 1596941017191630 exists.(#200) Permissions error – channasmcs Jan 29 '15 at 12:31
  • actually you need `publish_pages` instead of `publish_actions` now. – Highstrike Jun 08 '15 at 10:09
  • At the time of writing this was correct. But thanks for the hint! – Tobi Jun 08 '15 at 10:13