Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image to facebook fan page using API

I am trying to upload an image through my application with Facebook Graph API to an album of my fan page. Although I provide the albumID like a parameter for uploading the image, it is uploaded in an album named APPLICATION_NAME Photos on my own profile. The album of the fan page stays empty. I tried also with sending the pageID of my fan page instead of albumID, but the result is the same. The code I use for the upload is:

$fb_pfoto = $facebook->api('/' . $albumID . '/photos','POST', array(
       'access_token' => $session['access_token'],
       'message'      => 'Caption',
       'source'       => '@' . realpath( '/path/to/image.jpg' )
    ));

Please give me ideas how can I upload the image to the fan page and not to my own profile album.

like image 885
o15a3d4l11s2 Avatar asked Sep 23 '10 12:09

o15a3d4l11s2


People also ask

Can you post to Facebook using API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

How do I share a Facebook post with API?

Yes, you can share using the graph2 api. The way you do it is to use /feed edge and pass the post's url that you want to share as the link. Standard Fb permissions to the post you are sharing do apply. This was done today, in a local rails app, using FbGraph2 gem, with the above method.


3 Answers

Here is the script for uploading photos on your Facebook Fanpage:

<html>
<head>
<title>WebSpeaks.in | Upload images to Facebook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
require_once 'library/facebook.php';
$facebook = new Facebook(array(
 'appId'  => $app_id,,
 'secret' => $app_secret,
 'fileUpload' => true
));


//It can be found at https://developers.facebook.com/tools/access_token/
$access_token = '<Your access token>';

$params = array('access_token' => $access_token);

//The id of the fanpage
$fanpage = '330299184793';

//The id of the album
$album_id ='10150418901414794';

//Replace arvind07 with your Facebook ID
$accounts = $facebook->api('/arvind07/accounts', 'GET', $params);

foreach($accounts['data'] as $account) {
 if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
  $fanpage_token = $account['access_token'];
 }
}


$valid_files = array('image/jpeg', 'image/png', 'image/gif');

if(isset($_FILES) && !empty($_FILES)){
 if( !in_array($_FILES['pic']['type'], $valid_files ) ){
  echo 'Only jpg, png and gif image types are supported!';
 }else{
  #Upload photo here
  $img = realpath($_FILES["pic"]["tmp_name"]);

  $args = array(
   'message' => 'This photo was uploaded via WebSpeaks.in',
   'image' => '@' . $img,
   'aid' => $album_id,
   'no_story' => 1,
   'access_token' => $fanpage_token
  );

  $photo = $facebook->api($album_id . '/photos', 'post', $args);
  if( is_array( $photo ) && !empty( $photo['id'] ) ){
   echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
  }
 }
}

?>
 <!-- Form for uploading the photo -->
 <div class="main">
  <p>Select a photo to upload on Facebook Fan Page</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  <p>Select the image: <input type="file" name="pic" /></p>
  <p><input class="post_but" type="submit" value="Upload to my album" /></p>
  </form>
 </div>
</body>
</html>

You can watch complete tutorial here.

like image 81
Arvind Bhardwaj Avatar answered Nov 15 '22 20:11

Arvind Bhardwaj


Don't know if this would help but maybe you need to request manage_pages extended permission and then use returned page access_token for posting pictures. You can read briefly about this process here.

Then you can try these links with page access token for posting photos:

/me/photos
/<page_id>/photos
/<album_id>/photos
like image 37
serg Avatar answered Nov 15 '22 19:11

serg


My best guess would be one of two possibilties:

  • that your app does not have permission to upload to that album. If you request /me/albums using the pages access token, it will list the albums associated with that page. The is a can_upload field for each album which is either true or false (specific to the access token you are using).

    or

  • You are using an access token associated with your personal account, and not the page the album belongs to. Here is some information on how to authenticate as a page rather then the user who owns the page: http://developers.facebook.com/docs/authentication/pages/

like image 34
thefreeman Avatar answered Nov 15 '22 21:11

thefreeman