0

好的,所以下面的鳕鱼基本上可以正常工作,当这条线是这样的:

$post_url = '/'.$userPageId.'/feed';

这成功发布到 Facebook 页面时间轴。但我想上传一张照片,当我把这行改成

$post_url = '/'.$userPageId.'/photos';

它发布照片......但它发布到用户提要,而不是作为 Facebook 页面。

我希望这可以将照片发布到 Facebook 页面时间轴

[配置.php]

<?php
include_once("inc/facebook.php");

##################################
//Call Facebook API

// Required facebook permissions
$fbPermissions = 'publish_stream,manage_pages,photo_upload';

$facebook = new Facebook(array(
  'appId'  => $appId,
  'secret' => $appSecret,
'fileUpload' => true,
'cookie' => true  
));

$fbuser = $facebook->getUser();
?>

[进程.php]

<?php
include_once("config.php");

if($_POST)
{
    //Post variables we received from user
    $userPageId         = $_POST["userpages"];
    $userMessage         = $_POST["message"];

    if(strlen($userMessage)<1) 
    {
            //message is empty
            $userMessage = 'No message was entered!';
    }

    //HTTP POST request to PAGE_ID/feed with the publish_stream
    $post_url = '/'.$userPageId.'/photos';

    //posts message on page statues 
    $msg_body = array(
        'source' => '@' . 'test.jpg',                   
        'message' => "yo yo yo",
    );

    if ($fbuser) {
        try {
            $postResult = $facebook->api($post_url, 'post', $msg_body );
        } catch (FacebookApiException $e) {
            echo $e->getMessage();
        }
    } else {
        $loginUrl = $facebook->getLoginUrl(
            array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)
        );
        header('Location: ' . $loginUrl);
    }

    //Show sucess message
    if($postResult)
    {

    }
}
?>

[index.php](用于登录)

<?php
include_once("config.php");
if ($fbuser)
{
    try {
        $user_profile = $facebook->api('/me');
        //Get user pages details using Facebook Query Language (FQL)
        $fql_query = 'SELECT page_id, name, page_url FROM page '
            .'WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid='
            .$fbuser.')';
        $postResults = $facebook->api(
            array( 'method' => 'fql.query', 'query' => $fql_query )
        );
    } catch (FacebookApiException $e) {
        echo $e->getMessage();
        $fbuser = null;
    }
} else {
    //Show login button for guest users
    $loginUrl = $facebook->getLoginUrl(
        array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)
    );
    echo '<a href="'.$loginUrl.
        '"><img src="images/facebook-login.png" border="0"></a>';
    $fbuser = null;
}

if($fbuser && empty($postResults))
{
    /*
    if user is logged in but FQL is not returning any pages, we need to make
    sure user does have a page OR "manage_pages" permissions isn't granted yet
    by the user. Let's give user an option to grant permission again.
    */
    $loginUrl = $facebook->getLoginUrl(
        array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)
    );
    echo '<br />Could not get your page details!';
    echo '<br /><a href="'.$loginUrl.'">Click here to try again!</a>'; 
}
elseif ($fbuser && !empty($postResults))
{
    //Everything looks good, show message form.
}
?>
4

1 回答 1

4

如果您想充当 Facebook 主页并上传照片,您需要:

  1. 成为 Facebook 页面的管理员
  2. 获得授权时至少有以下范围:'manage_pages'、'photo_upload'
  3. 访问令牌作为 Facebook 页面,而不是作为管理员的用户

您可以通过调用Open Graph API来获取您作为管理员的 Facebook 页面的访问令牌:

$accounts = $facebook->api('/me/accounts','GET');

为了获得访问令牌和更多信息,您需要“manage_pages”权限。

获得访问令牌后,您需要使用它并使用它重新初始化客户端。然后,您基本上可以充当它,并张贴照片。希望这足以让你开始。您的代码看起来已设置为能够将照片上传到您想要的位置。这都是访问令牌的问题。

编辑:

您可以使用 PHP SDK 使用setAccessToken设置访问令牌:

// Set a new access token, by first getting it via means other than the SDK
$facebook->setAccessToken($new_access_token);
于 2013-05-15T22:33:24.390 回答