I\'m writing an application that uploads a photo to facebook. I\'m not supplying an album ID so an album is created on behalf of my app.
My question is: how do I get
Jimmy Sawczuk's answer is close, but doesn't work because FQL Photo table columns aid
and pid
are Rest/FQL API ids, not Graph API ids.
Fortunately the object_id
and album_object_id
columns in this table are indexable (contrary to the documentation) so this should give you both Rest/FQL & Graph ids for the album:
select aid, album_object_id from photo where object_id = ${GRAPH_PHOTO_ID}
In general object_id
fields in Rest/FQL API responses are Graph API id (except for photo notifications, where it's the Rest/FQL id).
Re HonkyHonk's answer: not all photo link
urls have a set
parameter; in this case I think you can find the Rest/FQL API photo id from its other parameters, and then use Rest/FQL API to get the album id:
(id << 32) | pid
2360873244068853905
id
, _
and pid
100002937903251_155052
It would help a lot if Facebook could document something about the two types of ids. Also the Graph API Photo schema should include the album's Graph API id.
function get_album_id_by_album_name($album_name){//return array of data
$fql = 'SELECT aid, owner, name, object_id FROM album WHERE owner=me() and name="'.$album_name.'"';
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $this->facebook->api($param);
return $fqlResult;
}
Richard Barnett's solution works well for me, I use this as an FQL query through the Graph API to get a JSON response like I would for any other Graph API query:
https://graph.facebook.com/fql?q=select+album_object_id+FROM+photo+WHERE+object_id='[PHOTOID]'
This returns:
{
"data": [
{
"album_object_id": "10150531092908837"
}
]
}
Then you can access the album ID using:
<?php
$strPhotoAlbumID = $objFacebookAPI->data[0]->album_object_id;
?>
This is the cleanest solution i've found as Facebook constantly change the format of their photo and album URLs so parsing these with RegEx wont work as a long term fix.
I ended up just using the legacy REST api since the album ID is passed with the link to the image in the server response. I then used a regex to pull the id out.
Not the best solution and I wouldn't recommend it but I feel its better than adding another several server requests to the code.
Long, clunky, but theoretically valid way:
http://graph.facebook.com/userid/albums
will get you a list of all the albums the user lets you see, then you can search the album objects (api here) for created_times
that match the time you created the album, which you can store when you create the album.
This is almost certainly a horrible way to do it, but it should work.
If I'm understanding you correctly, you have the photo ID but need the album ID. You could try using the photo FQL table with the following query:
SELECT aid, pid FROM photo WHERE pid = '<your photo id>'