Reputation: 3405
I'm trying to make a page that my mobile phone app can hit to post an image to Facebook. It looked like my images were just disappearing into the void, but I finally figured out what was happening (thanks phwd!)
The images are being uploaded properly, the problem is that the images have to be approved before they show up in the album. This wouldn't be so bad, except that if an album doesn't contain any approved images, it won't show up in your albums list when you click on Photos (thanks for the catch-22, FB!); this means there's no way to get to the images to approve them other than being given a direct link to them or the album.
Supposedly it's possible to post images and have them immediately show up, rather than needing to be approved by the user, if you have the correct permissions, as described in the answer here.
The app has sandbox mode enabled, and I'm using test users with the following permissions: read_stream
, user_photos
, manage_pages
, photo_upload
, and publish_stream
.
My understanding was that the publish_stream
permission lets me create a new album for the photos if it doesn't already exist, but images uploaded with just that permission have to be approved by the user. The user_photos
permission apparently should be letting me bypass user approval but that doesn't seem to be happening for some reason. Does anyone have any suggestions?
Code:
<?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' => ''));
?>
Upvotes: 2
Views: 2461
Reputation: 3405
Turning off sandbox mode apparently fixes this, though I have no idea why. I was under the impression that sandbox mode just kept you from interacting with normal users?
Will accept any answer that properly explains this, just putting this one up so people see the solution I found.
Upvotes: 3