作为主页(不是个人)发布到 Facebook 主页 [英] Posting to a Facebook Page as the Page (not a person)

查看:26
本文介绍了作为主页(不是个人)发布到 Facebook 主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是我修改后用来存档的example.php:

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.

浏览了整个 API..

Went through the whole API..

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

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

$facebook->setAccessToken($page_access_token);

就是这样做的,之后一切都按预期进行,无需修改发布功能并添加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.

推荐答案

1.首先你必须获得页面访问令牌.

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

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

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.

这篇关于作为主页(不是个人)发布到 Facebook 主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆