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

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

问题描述

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



我尝试用access_token替换access_token我从页面(/ me / accounts)获取,但现在说由于某些原因令牌无效。
Facebook模拟页面目前离线,我没有看到任何有关我想做的事情的API中的信息。也许我迷失了方向,或者看不到正确的方向..



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

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

//创建我们的应用程序实例(将其替换为您的appId和密码)。
$ facebook = new Facebook(array(
'appId'=>'xxxxxxxxxxxxx',
'secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxx'
));

//获取用户ID
$ user = $ facebook-> getUser();

//列出所有的应用程序和页面
if($ user){
try {
//继续知道你有一个登录的用户被认证。
$ 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'=&消息',
'link'=>'http://xxxxxxx.com',
'picture'=>'http://xxxxxx/xxxx_logo.gif',
' name'=>'xxxxxxxx在这里',
'description'=>'这里令人兴奋的描述!'

);
// as $ _GET ['publish']被设置为通过将用户重定向到基址url
} catch(FacebookApiException $ e){
echo($ e);
echo $ publishStream;
echo'catch to here';
}
}

?>

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






通过整个API ..



解决方案:



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

  $ facebook-> setAccessToken($ page_access_token); 

只是这样,之后一切都按照通常的预期,不需要修改帖子功能并添加access_token选项以发布。

解决方案

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&范围= manage_pages&安培; RESPONSE_TYPE =记号;
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 type =text / javascript>
var token ='<?php echo $ page_access_token?>';

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

名称:'xxxxx',
标题:'xxxxxx',
描述:'xxxxxxx',
} ;

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

3.请注意,此代码只会在粉丝专页的墙上发布您的帖子,并且喜欢粉丝专页的用户



希望这将解决您的问题。


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.

解决方案

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.

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

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