Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload images to a specific Facebook Album

I can create an album using facebook graph api by posting data to

http://graph.facebook.com/ALBUM_ID/albums

it returns an id, which is not the album id.

I confirmed it in two ways

  1. by going to that album, aid is different than what id i received
  2. and by posting http://graph.facebook.com/{ALBUM_ID_I_RECIEVED}/photos, but the photo is never published to newly created album instead it is published to a default application album.

I need to know the real album id so that i can upload images to newly created album.

like image 381
Imran Naqvi Avatar asked Dec 22 '10 12:12

Imran Naqvi


2 Answers

The new php-sdk is working just fine, I've just tested it and here are my comments:

1.Make sure you have the right extended permission which is publish_stream for both Album creation and Photo upload.

2.After creating the Album using the below code, you'll receive the Album ID:

$post_data = array(
    'name'=>"My Test Album",
    'description'=>"My Test Album Desc"
    );
$data['album'] = $this->facebook->api("/me/albums", 'post', $post_data);  

P.S: I confirm this ID is NOT the ID you see when browsing Facebook

3.Now we need to upload the image with the Album "reference" ID you got:

$file = FCPATH . "assets/img/small1.jpg";
$post_data = array(
    "message" => "My photo caption",
    "source" => '@' . realpath($file)
);
$album_id = "473425002142";
$data['photo'] = $this->facebook->api("/$album_id/photos", 'post', $post_data);  

P.S: the Constant FCPATH is a Codeigniter constant, you can you $_SERVER['DOCUMENT_ROOT'] or whatever method you are comfortable with.

And the result:
alt text

like image 111
ifaour Avatar answered Oct 14 '22 18:10

ifaour


I have actually faced the exact same problem, but with Events. What happens is Facebook does not return the actual event ID (in your case, the Album ID), but instead there's a small offset (with a maximum of 10). So, here's the workflow you need to follow (in order to make it work!):

  1. Publish the album. Save the returned (fake) album id, fb_aid in your database.
  2. Query for all published albums of the current FB-logged-in user.
  3. Loop through the list of returned albums.
  4. Identify the actual album id as follows by testing if the offset is < 10.
  5. Save actual album id in the database.

$fbAlbums = $this->facebook->api(array('method' => 'photos.getalbums'));
$myAlbums = $this->Album->find('all');
for ($i = 0; $i < count($fbAlbums); ++$i) {    
    foreach ($myAlbums as $myAlbum) {
        $aidOffset = abs($fbAlbums[$i]['aid'] - $myAlbum['Album']['fb_aid']);
        if ($aidOffset != 0     // Prevents fixing an already fixed album id
            && $aidOffset < 10  // Checks if this is a FB ID Screw up
           ) {
            $myAlbum['Album']['fb_aid'] = $fbAlbums[$i]['aid'];
            $this->Album->save($myAlbum);
            break; // Little optimization
        }
    }
 }

Now your database has updated album IDs. Hurray! Developer: 1. Facebook API: 0.

I know it's not pretty. I know there are so many potential problems with it. But it gets the job done until Facebook cares enough to fix its API.

like image 20
RabidFire Avatar answered Oct 14 '22 16:10

RabidFire