2

我创建了一个比赛,提交的表格将:

  1. 在 Facebook 员工的墙上写下评论和
  2. 在我的页面墙上写评论

我对第 1 步没有任何问题,但第 2 步不起作用。我的代码如下:

连接.php

<?php
//include the Facebook PHP SDK
include_once 'facebook.php';

//instantiate the Facebook library with the APP ID and APP SECRET
$facebook = new Facebook(array(
    'appId' => 'CRYPT FOR THIS FORUM',
    'secret' => 'CRYPT FOR THIS FORUM',
    'cookie' => true
));

//Get the FB UID of the currently logged in user
$user = $facebook->getUser();

//if the user has already allowed the application, you'll be able to get his/her FB UID
if($user) {
    //start the session if needed
    if( session_id() ) {

    } else {
        session_start();
    }

    //do stuff when already logged in

    //get the user's access token
    $access_token = $facebook->getAccessToken();
    //check permissions list
    $permissions_list = $facebook->api(
        '/me/permissions',
        'GET',
        array(
            'access_token' => $access_token
        )
    );

    //check if the permissions we need have been allowed by the user
    //if not then redirect them again to facebook's permissions page
    $permissions_needed = array('publish_stream', 'read_stream');
    foreach($permissions_needed as $perm) {
        if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 ) {
            $login_url_params = array(
                'scope' => 'publish_stream,read_stream',
                'fbconnect' =>  1,
                'display'   =>  "page",
                'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
            );
            $login_url = $facebook->getLoginUrl($login_url_params);
            header("Location: {$login_url}");
            exit();
        }
    }

    //if the user has allowed all the permissions we need,
    //get the information about the pages that he or she managers
    //id pag sposiamo è 494659577226200
    $accounts = $facebook->api(
        '/me/accounts',
        'GET',
        array(
            'access_token' => $access_token
        )
    );

    //save the information inside the session
    $_SESSION['access_token'] = $access_token;
    $_SESSION['accounts'] = $accounts['data'];
    //save the first page as the default active page
    //$_SESSION['active'] = $accounts['data'][0];*/

    //redirect to manage.php
    header('Location: manage.php');
} else {
    //if not, let's redirect to the ALLOW page so we can get access
    //Create a login URL using the Facebook library's getLoginUrl() method
    $login_url_params = array(
        'scope' => 'publish_stream,read_stream',
        'fbconnect' =>  1,
        'display'   =>  "page",
        'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
    );
    $login_url = $facebook->getLoginUrl($login_url_params);

    //redirect to the login URL on facebook
    header("Location: {$login_url}");
    exit();
}

?>

新帖子.php

<?php
//include the Facebook PHP SDK
include_once 'facebook.php';

//start the session if necessary
if( session_id() ) {

} else {
    session_start();
}

//instantiate the Facebook library with the APP ID and APP SECRET
$facebook = new Facebook(array(
    'appId' => 'CRYPT',
    'secret' => 'CRYPT',
    'cookie' => true
));

//get the info from the form
$parameters = array(
    'message' => $_POST['message'],
    'picture' => $_POST['picture'],
    'link' => $_POST['link'],
    'name' => $_POST['name'],
    'caption' => $_POST['caption'],
    'description' => $_POST['description']
);

//add the access token to it
$parameters['access_token'] = $_SESSION['active']['access_token'];

//build and call our Graph API request
$newpost = $facebook->api(
    '/494659577226200/feed',
    '/me/feed',
    'POST',
    $parameters
);

//redirect back to the manage page
header('Location: manage.php');
exit();

494659577226200 = FBPAGEID

问题是“/494659577226200/feed”,并且错误 AuthCode 200...

4

1 回答 1

0

您需要让您的用户授予您的应用manage_pages发布到他们代表他们管理的页面的权限。在此处查看他们的权限文档,请参阅Page Permissions部分。

引用自文档: manage_pages

使您的应用程序能够检索用户管理的页面和应用程序的 access_tokens。可以通过 Graph API 调用 //accounts 来查询访问令牌。此权限仅与 Graph API 兼容,而不与已弃用的 REST API 兼容。请参阅此处以生成在 60 天后不会过期的长期页面访问令牌。

获得此权限后,您可以使用页面访问令牌发布墙帖

于 2012-10-11T02:23:03.073 回答