发布到粉丝页面会出错

时间:2013-02-11 02:21:13

标签: php facebook facebook-page

以下作品可以发布到Facebook FAN页面,但仅限于“其他人最近的帖子......”部分。如果我发布到正常的FACEBOOK页面,而不是FAN PAGE,则编码效果很好。

我已经尝试了每篇关于发布到粉丝页面的帖子。似乎没什么用。我的想法是我没有为用户提供实际页面的ACCESS TOKEN。或者我没有获得具有manage_pages权限的返回令牌。

感谢您提供的任何帮助。

<?php 
$app_id = xxxxxxxxxx; // APP ID
$app_secret = xxxxxxxxxxxx;
$my_url = "http : // www . afakewebsitefordemoonly.com/thispageURL.php";
$fb_id = xxxxxxxxxxx; // ACTUAL FAN PAGE NUMBER


// known valid access token stored in a database
$access_token = MANUAL_ACCESS_TOKEN; // GOT MANUAL ACCESS TOKEN FROM API Explorer - expires in about 90 minutes but good enough to request a new token. THE original TOKEN HAS manage_pages and publish_stream permissions.

$code = $_REQUEST["code"];

// If we get a code, it means that we have re-authed the user
//and can get a valid access_token.

if (isset($code)) {

    $token_url="https : // graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&client_secret=" . $app_secret
    . "&code=" . $code . "&display=popup";
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
}


// Attempt to query the graph:
$graph_url = "https : //graph.facebook.com/me?" . "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);

//Check for errors
if ($decoded_response->error) {
    // check to see if this is an oAuth error:
    if ($decoded_response->error->type== "OAuthException") {
        // Retrieving a valid access token.
        $dialog_url= "https : // www . facebook.com/dialog/oauth?"
        . "client_id=" . $app_id
        . "&redirect_uri=" . urlencode($my_url);
        echo("<script> top.location.href='" . $dialog_url
        . "'</script>");
    }
    else {
        echo "other error has happened";
    }
}
else {
    // success
    //echo("success" . $decoded_response->name);
    //echo($access_token);
    /******************************/
    //$data['from'] = $fb_id;
    //$data['picture'] = "http :// www. URL_TO_PHOTO.jpg";
    $data['link'] = "http  :// www .fakewebsitefortesting.com/"; // static link I want to put on post on my site.
    $data['message'] = "Message here.";
    $data['subject'] = 'Some Subject';
    $data['title'] = 'Fancy Title here';
    $data['description'] = "Description is more info here.";
    $data['access_token'] = $access_token;

    $post_url = 'https : // graph.facebook.com/'. $fb_id . '/feed';

    echo "Post URL: ". $post_url . "<br />";
    echo "Data: " . $data . "<br />";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $post_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($ch);
    curl_close($ch);
    echo "Return: ". $return . "<br />";
    /******************************/

}

// note this wrapper function exists in order to circumvent PHP’s
//strict obeying of HTTP error codes.  In this case, Facebook
//returns error code 400 which PHP obeys and wipes out
//the response.
function curl_get_file_contents($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
}

?>

1 个答案:

答案 0 :(得分:0)

您没有使用该页面的访问令牌,因此Facebook将您视为普通用户(而非页面管理员),并将您的帖子添加到“其他人最近的帖子”中。

要发布到页面,因为页面管理员使用适当的页面访问令牌。您可以通过对https://graph.facebook.com/me/accounts?access_token=YOUR_ACCESS_TOKEN

进行api调用来查找所有页面和访问令牌