【问题标题】:Posting to a Facebook Page as the Page (not a person)作为主页(不是个人)发布到 Facebook 主页
【发布时间】:2013-10-31 12:49:57
【问题描述】:

我使用 Facebook PHP SDK 3.0.1(目前最新)。 我需要做的是在页面上以页面的身份发布。

我尝试用从页面 (/me/accounts) 获得的 access_token 替换 access_token,但现在它说令牌由于某种原因无效。 Facebook“模拟”页面现在处于离线状态,我在 API 中看不到任何关于做我想做的事情的信息。也许我迷路了,或者没有寻找正确的方向。

这是我修改并用于存档的 example.php:

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';
            }
        }

?>

由于我无法回答自己的问题,因此我编辑了问题。


浏览了整个 API..

解决方案:

在作为页面发布之前,您需要将 access_token 设置为一个页面拥有。

$facebook->setAccessToken($page_access_token);

这样做了,之后一切都按预期进行,无需修改发布功能并在发布中添加“access_token”选项。

【问题讨论】:

标签: php facebook post facebook-graph-api


【解决方案1】:

1.首先你要获取页面访问令牌。

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.获取页面访问令牌后,只需将该令牌包含在您的贴墙代码中即可。

<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.请记住,此代码只会在粉丝页面的墙上发布您的帖子,喜欢粉丝页面的用户将能够看到该帖子,因为该帖子已发布在他们自己的 Feed 上。

希望这能解决您的问题。

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多