如何将照片上传到Facebook页面的时间表作为Facebook页面本身,而不是用户Feed [英] How to Upload a Photo to a Facebook Page's Timeline as the Facebook Page itself, Not to the user feed

查看:230
本文介绍了如何将照片上传到Facebook页面的时间表作为Facebook页面本身,而不是用户Feed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



确定所以鳕鱼以下基本上都可以正常工作,WHEN这行是这样的:

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

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

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

它发布照片...但它发布到用户Feed,而不是AS的Facebook页面。



我想要将照片发布到Facebook页面时间轴



[config.php]

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

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

//必需的Facebook权限
$ 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)
{
//我们从用户
$ userPageId = $ _POST [userpages]发送的变量
$ userMessage = $ _POST [message];

if(strlen($ userMessage)< 1)
{
//消息为空
$ userMessage ='没有消息输入!
}

//使用publish_stream
$ post_url ='/'.$userPageId.'/photos'向PAGE_ID / feed发送HTTP POST请求;

//页面上的帖子消息
$ msg_body = array(
'source'=>'@'。'test.jpg',
'message '=>哟哟哟,
);

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);
}

//显示sucess消息
if($ postResult)
{

}
}
?>

[index.php](用于登录)

 <?php 
include_once(config.php);
if($ fbuser)
{
try {
$ user_profile = $ facebook-> api('/ me');
//使用Facebook查询语言(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 {
//显示访客用户的登录按钮
$ loginUrl = $ facebook-> getLoginUrl(
array('redirect_uri'=> $ homeurl,'scope'=> $ fbPermissions)
);
echo'< a href ='$ loginUrl。
'>< img src =images / facebook-login.pngborder =0>< / a> ;';
$ fbuser = null;
}

如果($ fbuser&& empty($ postResults))
{
/ *
如果用户登录但FQL是没有返回任何页面,我们需要使
确保用户有一个页面或manage_pages权限未被授予
由用户。让我们给用户一个选项,再次授予权限。
* /
$ loginUrl = $ facebook-> getLoginUrl(
array('redirect_uri'=> $ homeurl,'scope'=> $ fbPermissions)
);
echo'< br />无法获取您的页面详细信息!
echo'< br />< a href ='。$ loginUrl。'>点击此处再试一次< / a>';
}
elseif($ fbuser&&!empty($ postResults))
{
//一切看起来不错,显示消息窗体。
}
?>


解决方案

如果您要作为Facebook页面并上传一张照片,您需要:


  1. 成为Facebook页面上的管理员

  2. 获得授权时至少有以下范围:'manage_pages','photo_upload'

  3. 访问令牌作为Facebook页面,而不是作为管理员的用户

您可以通过调用 Open Graph API

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

为了获取访问令牌和任何更多信息,您需要manage_pages权限。 / p>

一旦拥有访问令牌,您将需要使用它并重新初始化客户端。那么你可以基本上像它一样行事,并张贴一张照片。希望这将足以让你开始。您的代码看起来设置为能够将照片上传到您想要的位置。这是所有访问令牌的问题。



编辑:



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

  //设置一个新的访问令牌,首先通过SDK以外的方式获取它
$ facebook-> setAccessToken($ new_access_token);


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

解决方案

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);

这篇关于如何将照片上传到Facebook页面的时间表作为Facebook页面本身,而不是用户Feed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆