Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting to a Facebook Page as the Page (not a person)

I use Facebook PHP SDK 3.0.1 (latest currently). What I need to be able to do is to post as a the identity of the Page on the Page.

I tried replacing the access_token with the access_token I get from the page (/me/accounts) however it now says token is invalid for some reason. Facebook "impersonation" pages are offline now, and I don't see any info in the API regarding doing what I want.. maybe I'm lost or maybe not looking in the right direction..

Here's the example.php that I modified and use to archive this:

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'   => 'xxxxxxxxxxxxx',
  'secret'  => 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
));

// Get User ID
$user = $facebook->getUser();

//Lists all the applications and pages
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $accounts_list = $facebook->api('/me/accounts');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

$page_selected = 'xxxxxxxxxxxx';
$page_access_token = $accounts_list['data']['0']['access_token'];
echo 'page_access_token:' . $page_access_token;


<?php

if (isset($_GET['publish'])){
            try {
                $publishStream = $facebook->api("/$page_selected/feed", 'post', array(
                    'access_token'  => '$page_access_token',
                    'message'   => 'Development Test message',
                    'link'      => 'http://xxxxxxx.com',
                    'picture'   => 'http://xxxxxx/xxxx_logo.gif',
                    'name'      => 'xxxxxxxx Goes Here',
                    'description'=> 'And the exciting description here!'
                    )
                );
                //as $_GET['publish'] is set so remove it by redirecting user to the base url
            } catch (FacebookApiException $e) {
                echo($e);
                echo $publishStream;
                echo 'catch goes here';
            }
        }

?>

Since I can't answer my own question I edited the question.


Went through the whole API..

Solution:

Before posting as the page you need to set your access_token to the one page owns.

$facebook->setAccessToken($page_access_token);

does just that, and afterwards everything goes as it normally would be expected, no need to modify post function and add "access_token" option to post.

like image 722
tictac Avatar asked Jun 04 '11 21:06

tictac


People also ask

Can you post on a Facebook page as a page?

#2: Post on Facebook Pages as Your Page If you want to post directly to the wall of a Facebook page as your page (so your post shows up in the Visitor Posts box on the left sidebar of the page), use the drop-down at the top right of the status update box to select the page you want to comment as.

How do I change who I am commenting as on Facebook?

As shown below, click on your profile icon in the lower right corner with a little dropdown arrow next to the comment field. Select the name of the profile you want to comment as and write your comment.

Can you post on someone's Facebook page if you are not friends?

You can send a message to anyone on Facebook, regardless of friend status or privacy settings. The only exception applies to members you've blocked and those who've blocked you. Filtering preferences may inadvertently cause messages to go unseen, even though they have been delivered.


1 Answers

1.First you have to get the page access token.

public function getPageToken()
{

    $page_id = "xxxxxxxxxx";

    $page_access_token = "";

$result =  $this->facebook->api("/me/accounts");
if( !empty($result['data']) )
{
   foreach($result["data"] as $page) 
   {
     if($page["id"] == $page_id)
     {
       $page_access_token = $page["access_token"];
       break;
     }
   }
}
else
{
  $url = "https://www.facebook.com/dialog/oauth?client_id=xxxxxxxxxx&redirect_uri=http://apps.facebook.com/xxxxxx&scope=manage_pages&response_type=token";
  echo "<script type='text/javascript'> top.location.href='".$url."'; </script>";
}
return $page_access_token;
}

2.After getting the page access token just include that token in your post to wall code.

<script src="//connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
var token = '<?php echo $page_access_token ?>';

var wallPost = {
access_token: token,
message     : 'xxxxxxxx',
link        :  'http://apps.facebook.com/xxxxxxx/',
picture     : 'xxxxxxxx',

name        : 'xxxxx',
caption     : 'xxxxxx',
description : 'xxxxxxx',
      };

FB.api('/pageid/feed', 'post', wallPost, function(response) {
if (!response || response.error) {
} else {
}
});
</script>

3.Remember this code will only publish your post on Fan page's wall and users who liked the fan page will be able to see that post as the post is posted on their own feed.

Hope this will solve your problem.

like image 84
tariq Avatar answered Nov 15 '22 14:11

tariq