2012-01-05 85 views
2

我在將頁面添加爲頁面時遇到問題。 我使用下面的代碼:SDK:以頁面形式發佈

$app_id = "xxx"; 
$app_secret = "xxx"; 
$my_url = base_url().'admin/facebook/'; 

if(isset($_GET["code"])) { 
    $code = $_GET["code"]; 
} else { 
    $code = ''; 
} 
if(empty($code)) { 
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection 
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=manage_pages,publish_stream,offline_access&state=" 
    . $_SESSION['state']; 

echo("<script> top.location.href='" . $dialog_url . "'</script>"); 
} 

if($_GET['state'] == $_GET['state']) { 

$token_url = "https://graph.facebook.com/oauth/access_token?" 
    . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret . "&code=" . $code; 

$response = @file_get_contents($token_url); 
$params = null; 
parse_str($response, $params); 

$graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token']; 

$user = json_decode(file_get_contents($graph_url)); 
echo("Hello " . $user->name); 

    $pageID = '212154388861617'; 


    // post to wall (feed is wall post, just update to whatever you want to publish to) 
    try { 
     //153406078098666 
     $publishStream = $this->fb_ignited->api("212154388861617/feed", 'post', array(
      'message' => "Test message", 
      'access_token' => $params['access_token'] 
      ) 
     ); 
    } catch (FacebookApiException $e) { 
     die($e); 
    } 
} 
else { 
echo("The state does not match. You may be a victim of CSRF."); 
} 

的職位是正確添加我的網頁上,但該帖子被加爲我的用戶帳戶,而不是我的網頁賬戶。 我怎樣才能作爲一個頁面發佈? 或者這不可能嗎?

解決方案:我加入這一段代碼

$result = $this->fb_ignited->api("/me/accounts", array('access_token' => $params['access_token'])); 
     foreach($result["data"] as $page) { 
      if($page["id"] == $pageID) { 
       $page_access_token = $page["access_token"]; 
       break; 
      } 
     } 

回答

1

您使用用戶的access_token,而你需要使用access_token頁面。

這可以通過圖形API從useraccounts連接一旦用戶授予權限manage_pages您的應用程序

+0

THX這並獲得成功獲得。 – 2012-01-05 14:03:52