【问题标题】:Facebook PHP SDK: How can I post on page on behalf of pageFacebook PHP SDK:如何代表页面在页面上发布
【发布时间】:2014-05-27 12:48:38
【问题描述】:

我正在使用 facebook 的 php SDK,我正在尝试在由登录用户管理/管理的页面上发帖。我已授予 publish_stream 和 manage_pages 权限。而且我希望帖子看起来像是由页面而不是管理员或登录用户制作的。我已经尝试了几种帮助,但没有人在工作。这是我现有代码的一部分:

require './php-fb-sdk/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook( array('appId' => 'xxxx', 'secret' => 'zzzz'));

// Get User ID
$user = $facebook -> getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook -> api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}

$params = array("scope" => array("manage_pages", "publish_stream"));
// Login or logout url will be needed depending on current user state.
if ($user) {
    // Fetch the viewer's basic information
    $basic = $facebook -> api('/me');

    $permissions = $facebook -> api("/me/permissions");
    if (array_key_exists('manage_pages', $permissions['data'][0]) && array_key_exists('publish_stream', $permissions['data'][0])) {

        $admin_pages = $facebook -> api(array('method' => 'fql.query', 'query' => "SELECT page_id, type from page_admin WHERE uid=me() and type!='APPLICATION'"));
        if (count($admin_pages) > 0) {

            $post_info = $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
            echo '<hr>The post info is: ' . print_r($post_info, true) . '<hr>';
        } else {
            echo '<hr> You are not admin of any fb fan page<hr>';

        }
        //print_r($admin_pages);

    } else {
        // We don't have the permission
        // Alert the user or ask for the permission!
        header("Location: " . $facebook -> getLoginUrl(array("scope" => "manage_pages,publish_stream")));
    }
    $logoutUrl = $facebook -> getLogoutUrl();
} else {
    //$statusUrl = $facebook->getLoginStatusUrl();
    $loginUrl = $facebook -> getLoginUrl($params);
    $statusUrl = $loginUrl;
}

使用上面的代码我可以在页面上发布,但看起来它是由用户制作的。

但是,如果我使用 facebook JS SDK,我会看到帖子看起来像是由页面制作的。

var data=
            {
                caption: 'My Caption',
                message: 'My Message'
            }

            FB.api('/' + pageId + '/feed', 'POST', data, onPostToWallCompleted);
            }

任何帮助或建议将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    要代表页面在页面上发帖,您需要使用页面访问令牌。您当前的通话:

    $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
    

    正在使用默认(用户)访问令牌。

    要获取页面访问令牌,请在发布帖子之前进行此调用:

    \GET /{page-id}?fields=access_token
    

    这将在结果中为您获取页面访问令牌,然后只需使用它来进行发布提要调用,就像这样-

    $facebook -> api(
      '/' . $admin_pages[0]["page_id"] . '/feed', 
      'post', 
      array(
        "caption" => "From web", 
        "message" => "This is from my web at: " . time(),
        "access_token" => '{page_access_token}'
      )
    );
    

    (如果需要,您还可以获得页面的永不过期令牌,请在此处查看如何:What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 2012-06-13
      • 2023-04-07
      • 2012-10-10
      • 1970-01-01
      相关资源
      最近更新 更多