Keyur Padalia
Keyur Padalia

Reputation: 2097

How to Publish on Facebook Page automatic from php server scripts?

My client has a e-commerce website where they sell their products and they also have facebook page for that website. I want to to publish wall post on that page when ever the product price is reduced or new product is lunched. But i want to make this automatic, so i want to authorize facebook automatic, means that i don't want any login dialog box. the script should authorize itself. now the code i am using is as below but it ask me for log in. and also tell me which method is good(easy) to publish page wall post?

require 'API_Library/Facebook/src/facebook.php';
 $page_id = '111111111111111';
 $appId = '111111111111111';
 $appSecret = 'aaaaaaaaaaaaaaaaaaaaa22222222222';

 $facebook = new Facebook(array(
             'appId' => $appId,
             'secret' => $appSecret,
             'cookie' => true
         ));
 $session = $facebook->getAccessToken();

 $me = NULL;

 if($session)
 {
     try
     {
         $me = $facebook->api('/me');
         $facebook->api('me/feed', 'post', array(
             'message' => 'Hello World!'
         ));
     }
     catch(FacebookApiException $e)
     {
         echo $e->getMessage();
     }
 }

OUTPUT: (#200) The user hasn't authorized the application to perform this action

Upvotes: 1

Views: 4791

Answers (1)

Niraj Shah
Niraj Shah

Reputation: 15457

In order to post something to a page, you will need the administrator of the page to be authenticated with the application. You cannot post to a page without being authenticated. So there will be some user interaction required before you can set this up automatically.

The process would be as follows:

  1. Setup a Facebook App to get a App ID and app secret - you'll need this to query the API and post updates to the page
  2. As the page administrator, login to your app with the manage_pages permission.
  3. After logging in, an API call to /me/accounts will give you an access_token to access the page and publish updates
  4. Using the page's accesss_token, you can then POST updates using /{page_id}/feed

Hope this helps.

Upvotes: 1

Related Questions