user1141142
user1141142

Reputation:

Post to FB Page as User

So I have a Facebook Page (let's call it X), which has app Y on it. The user can ask a question via Y, and it gets posted to X as the user (not as the app).

My permissions for the app are currently set to publish_stream.

I can grab a token via

    $token_url = "https://graph.facebook.com/oauth/access_token?" .
       "client_id=" . $this -> data["environment"] -> fb_appid .
       "&client_secret=" . $this -> data["environment"] -> fb_appsecret .
       "&grant_type=client_credentials";
    $app_token = file_get_contents($token_url);

which gives me a token just fine.

Now, if I try to POST via an APi call, I get two results:

When I do not pass the token and simply call

$post_id = $this ->Facebook->fb_api("/PAGE_ID/feed", "POST", array("message"=>"This is a post from PHP."));

I get a response back in the form of JSON

{
   "id": "PAGEID_someOtherID"
}

but I do not see the post on the wall.

When I do pass the access token, ala

$post_id = $this ->Facebook->fb_api("/PAGE_ID/feed", "POST", array("access_token"=>$app_token,"message"=>"This is a post from PHP."));

my response comes back empty.

What am I doing wrong with such a simple concept?

Upvotes: 0

Views: 893

Answers (1)

Pooya Estakhri
Pooya Estakhri

Reputation: 1289

Sample from Facebook docs https://developers.facebook.com/docs/reference/php/facebook-api/

<?php
      // Remember to copy files from the SDK's src/ directory to a
      // directory in your application on the server, such as php-sdk/
      require_once('php-sdk/facebook.php');

      $config = array(
        'appId' => 'YOUR_APP_ID',
        'secret' => 'YOUR_APP_SECRET',
      );

      $facebook = new Facebook($config);
      $user_id = $facebook->getUser();
    ?>
    <html>
      <head></head>
      <body>

      <?
        if($user_id) {

          // We have a user ID, so probably a logged in user.
          // If not, we'll get an exception, which we handle below.
          try {
            $ret_obj = $facebook->api('/'.$pageid.'/feed', 'POST',
                                        array(
                                          'link' => 'www.example.com',
                                          'message' => 'Posting with the PHP SDK!'
                                     ));
            echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

          } catch(FacebookApiException $e) {
            // If the user is logged out, you can have a 
            // user ID even though the access token is invalid.
            // In this case, we'll get an exception, so we'll
            // just ask the user to login again here.
            $login_url = $facebook->getLoginUrl( array(
                           'scope' => 'publish_stream'
                           )); 
            echo 'Please <a href="' . $login_url . '">login.</a>';
            error_log($e->getType());
            error_log($e->getMessage());
          }   
          // Give the user a logout link 
          echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
        } else {

          // No user, so print a link for the user to login
          // To post to a user's wall, we need publish_stream permission
          // We'll use the current URL as the redirect_uri, so we don't
          // need to specify it here.
          $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
          echo 'Please <a href="' . $login_url . '">login.</a>';

        } 

      ?>      

      </body> 
    </html>  

Well you are not able to post something on a page wall without access_token Cause without access token facebook can not realize Who is The user who wants to post & about the time you are trying to post via access_token i think you are doing something wrong please check permissions you have (you need publish_stream not stream_publish) i will put a sample code for you soon

Upvotes: 2

Related Questions