Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...