php 使用 API 将图片上传到 Facebook 粉丝页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3778424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:03:46  来源:igfitidea点击:

Upload image to facebook fan page using API

phpfacebookfacebook-graph-api

提问by o15a3d4l11s2

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:

我正在尝试通过我的应用程序使用 Facebook Graph API 将图像上传到我的粉丝页面的相册。虽然我提供了相册 ID 作为上传图像的参数,但它上传到我自己的个人资料中名为 APPLICATION_NAME 照片的相册中。粉丝专页的相册保持空白。我也尝试发送我的粉丝页面的 pageID 而不是专辑 ID,但结果是一样的。我用于上传的代码是:

$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.

请给我一些想法,如何将图像上传到粉丝页面而不是我自己的个人资料相册。

采纳答案by serg

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

不知道这是否有帮助,但也许您需要请求manage_pages扩展权限,然后使用返回的页面 access_token 发布图片。您可以在此处简要了解此过程。

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

然后你可以尝试使用页面访问令牌的这些链接来发布照片:

/me/photos
/<page_id>/photos
/<album_id>/photos

回答by Arvind Bhardwaj

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

这是在您的 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.

你可以在这里观看完整的教程

回答by thefreeman

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/

  • 您的应用无权上传到该相册。如果您使用页面访问令牌请求 /me/albums,它将列出与该页面关联的专辑。这是每个专辑的 can_upload 字段,它是 true 或 false(特定于您正在使用的访问令牌)。

    或者

  • 您使用的是与您的个人帐户相关联的访问令牌,而不是相册所属的页面。以下是有关如何作为页面而不是拥有页面的用户进行身份验证的一些信息:http: //developers.facebook.com/docs/authentication/pages/

回答by Hazemdido

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 thefreemanI solved the problem As thefreeman said , You must first take manage_pages permission then get Page Access Token as mentioned herethen 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"

我遇到了同样的难题,当我尝试将照片分享到页面时,我以用户而不是页面的身份发布了它。当我尝试将它发布到相册中时,照片发布在我的个人资料中。但是多亏了弗里曼,我解决了这个问题正如弗里曼所说,您必须首先获得manage_pages 权限, 然后获取此处提到的页面访问令牌,然后在您的应用程序或网站中使用此令牌而不是用户访问令牌最后,将照片发布到页面墙上“我/照片”或将其发布在页面中的特定相册中,请使用“{album-id}/photos”