2011-06-04 64 views
16

我使用Facebook PHP SDK 3.0.1(當前最新版本)。 我需要做的是將頁面標識作爲頁面的標識進行發佈。發佈到Facebook頁面作爲頁面(不是一個人)

我試着用我從頁面獲得的access_token替換access_token(/ me/accounts),但是現在說的令牌由於某種原因是無效的。 Facebook的「模仿」頁面現在脫機,並且我在API中看不到任何有關做我想做的事情的信息..也許我迷路了,或者可能看不到正確的方向。 .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」選項後。

+0

方式太多的代碼讓我閱讀 – Ibu 2011-06-04 21:23:11

+0

對不起,修剪了代碼。 – tictac 2011-06-04 21:26:02

+0

本教程應該有所幫助。它顯示瞭如何列出帳戶中的網頁:http://net.tutsplus.com/tutorials/php/wrangling-with-the-facebook-graph-api/comment-page-1/#comment-390691 – LeeTee 2011-12-13 17:04:12

回答

2

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.Remember這個代碼將只發布您的風扇頁的牆壁和誰喜歡粉絲頁面就能看到該職位的職位發佈在自己的飼料用戶發佈。

希望這會解決您的問題。

相關問題