0

以下内容可以很好地发布到 Facebook 粉丝页面,但仅限于“其他人在...上发布的最新帖子”部分。如果我发布到普通的 FACEBOOK 页面,编码效果很好,而不是 FAN PAGE。

我已经尝试了所有我能找到的关于发布到粉丝页面的帖子。似乎没有任何效果。我的想法是我没有获得只有用户的实际页面的访问令牌。或者我没有获得具有 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;
}

?>
4

1 回答 1

0

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

要以页面管理员身份发布到页面,请使用页面的适当访问令牌。您可以通过对https://graph.facebook.com/me/accounts?access_token=YOUR_ACCESS_TOKEN进行 api 调用来找到所有页面和访问令牌

于 2013-02-11T06:53:01.480 回答