How to Upload a Photo to a Facebook Page's Timeline as the Facebook Page itself, Not to the user feed

自作多情 提交于 2019-12-14 03:14:31

问题


OK so the cod below basically works fine, WHEN this line is like this:

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

This successfully posts to the Facebook Page Timeline. But I want to upload a photo, and when I change this line to

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

It posts the photo... but it posts to the User Feed, instead of AS the Facebook Page.

I want this to post the photo to the Facebook Page Timeline

[config.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();
?>

[process.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] (for logging in)

<?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.
}
?>

回答1:


If you want to act as the Facebook Page and upload a photo as such, you'll need:

  1. To be an Admin on the Facebook Page
  2. At least the following scopes when getting authorizations: 'manage_pages', 'photo_upload'
  3. The access token as the Facebook Page, NOT as the User who is the Admin

You can fetch the access tokens of the Facebook Pages you are Admin of by calling the Open Graph API:

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

In order to get the access token and any more information, you need the 'manage_pages' permission.

Once you have the access token, you'll need to use it and re-initialize a client with it. Then you can essentially act as it, and post a photo as it. Hopefully this will be enough to get you started. Your code looks set up to be able to upload the photo to where you want it to be. It's all a matter of the access token.

Edit:

You can set the access token with the PHP SDK with setAccessToken:

// Set a new access token, by first getting it via means other than the SDK
$facebook->setAccessToken($new_access_token);


来源:https://stackoverflow.com/questions/16575946/how-to-upload-a-photo-to-a-facebook-pages-timeline-as-the-facebook-page-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!