2

我正在尝试制作一个页面,我的手机应用程序可以点击该页面将图像发布到 Facebook。看起来我的图像刚刚消失在虚空中,但我终于弄清楚发生了什么(感谢 phwd!)

图像正在正确上传,问题是图像必须先获得批准才能显示在相册中。这不会那么糟糕,除非相册不包含任何已批准的图像,当您单击照片时它不会显示在您的相册列表中(感谢第 22 条,FB!);这意味着除了获得指向它们或相册的直接链接之外,没有其他方法可以访问图像来批准它们。

假设如果您拥有正确的权限,则可以发布图像并立即显示它们,而不需要用户批准,如答案here中所述。

该应用程序启用了沙盒模式,我正在使用具有以下权限的测试用户:read_streamuser_photosmanage_pagesphoto_uploadpublish_stream

我的理解是,publish_stream如果照片尚不存在,该权限允许我为照片创建一个新相册,但仅使用该权限上传的图像必须得到用户的批准。该user_photos许可显然应该让我绕过用户批准,但由于某种原因似乎没有发生。有没有人有什么建议?

代码:

<?php

// facebook variables
require_once("fbsdk/facebook.php");
$fbconfig = array();
$fbconfig['appId'] = '...';
$fbconfig['secret'] = '...';
$fbconfig['fileUpload'] = true;

// set up FB connection
$facebook = new Facebook($fbconfig);
$facebook->setAccessToken($_POST['access']);

// some database stuff
$image = 'image name goes here';

// verify access token
$user = $facebook->getUser();
if (!$user)
    die(json_encode(array('result' => '0',
        'message' => 'Invalid FB access token.')));

// see if target album exists
try {
    $album = $facebook->api(array(
        'query' => "SELECT object_id FROM album WHERE owner=me()' 
            . ' AND name='The App With No Name' AND can_upload=1",
        'method' => "fql.query"));

    // if it exists, grab its ID
    if (isset($album[0]['object_id']))
        $album_id = $album[0]['object_id'];
    else {
        // otherwise, create it
        $create_album = $facebook->api('/me/albums', 'post', array(
            'message' => 'My photos from The App With No Name',
            'name' => 'The App With No Name'));
        $album_id = $create_album['id'];
    }
} catch (FacebookApiException $e) {
    error_log($e->getType());
    error_log($e->getMessage());
    die(json_encode(array('result' => '0',
        'message' => 'FB call (query/create albums) failed: '
            . $e->getType() . ' | ' . $e->getMessage())));
}

// try to post the photo
try {
    $fbresult = $facebook->api('/' . $album_id . '/photos', 'POST',
        array('source' => '@' . realpath('../images/' . $image . '.jpg'),
            'message' => 'Shared from The App With No Name'));
} catch (FacebookApiException $e) {
    error_log($e->getType());
    error_log($e->getMessage());
    die(json_encode(array('result' => '0',
        'message' => 'FB call (post image) failed: '
            . $e->getType() . ' | ' . $e->getMessage())));
}

// more database stuff

// report success
echo json_encode(array('result' => '1', 'message' => ''));

?>
4

1 回答 1

3

关闭沙盒模式显然可以解决这个问题,但我不知道为什么。我的印象是沙盒模式只是让您无法与普通用户互动?

将接受任何正确解释这一点的答案,只需将其放上来,以便人们看到我找到的解决方案。

于 2012-07-07T17:32:08.350 回答