【问题标题】:SDK: Post as page on a pageSDK:在页面上发布为页面
【发布时间】:2012-01-05 12:39:09
【问题描述】:

我在将页面上的帖子添加为页面时遇到问题。 我使用以下代码:

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

【问题讨论】:

    标签: facebook facebook-graph-api post sdk facebook-php-sdk


    【解决方案1】:

    您正在使用用户的access_token,而您需要使用页面access_token

    一旦用户向您的应用授予了manage_pages 权限,就可以从useraccounts 连接通过Graph API 获得此信息

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2012-06-13
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多