开发者

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, i开发者_Python百科t 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.


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.


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


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/


I had the same hard problem that when I try to share a photo to a page ,I posted it as a user not as page .And when I try to post it in an album the photo posted in my profile. But thanks to thefreeman I solved the problem As thefreeman said , You must first take manage_pages permission then get Page Access Token as mentioned here then use this token instead of user access token in your app or website finaly ,to post the photos on the page wall"me/photos" or to post it in specific album in the page use "{album-id}/photos"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜