masa
masa

Reputation: 21

How to post photos into the album of Facebook page(fanpage) using API?

I'm building a Facebook page as administrator. And developing some apps using PHP and Facebook API. What I'd like to do is to upload photos into the specified album on the Facebook page. At this moment, I've got following error message after posting into the album_id.

"error": { "type": "OAuthException", "message": "(#120) Invalid album id"

Then, I'd like to know follofing things. How to get the access_token for the Facebook page not for me. The permissions (user_photos,publish_stream,manage_pages,photo_upload) are enough? Finally, if you have some experience, please show me the successful sample code?

Upvotes: 0

Views: 1049

Answers (1)

Jonathan
Jonathan

Reputation: 67

To get the access token for the page, try this:

$page_token_url = "https://graph.facebook.com/" . 
  $page_id . "?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];

For permissions publish_stream and manage_pages are enough.

For actually posting the image, I'm doing this (which works):

$ch = curl_init("https://graph.facebook.com/$album_id/photos/");  
curl_setopt($ch, CURLOPT_POSTFIELDS,
    array('access_token'=>$page_access_token,
    'source'=>"@$filename",
    'message'=>$caption,
    'description'=>$message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rtn = curl_exec($ch);
curl_close($ch);
echo $rtn;

Upvotes: 1

Related Questions