facebook create app for page to upload photos to page's photo album
I'm completely new to Facebook app development, so please bear with me.
I've been asked to create a Facebook Page, with an application that will let users upload photos into one of the Page's albums.
I've managed to create the page, let's call it MyPage
. I've also created an app MyApp
, and I've added the App to the profile of the Page. Now, when you view MyPage
profile, on the left (tabs), you will already find MyApp
.
MyApp
points to a php page on my hosting account. It has a form with a file field:
<form action="<?=$PHP_SELF;?>" enctype="multipart/form-data" method="post">
<input name="MAX_FILE_SIZE" type="hidden" value="10000000" />
<input id="fileSelect" name="fileSelect" type="file" />
<input name="submit" type="submit" value="Upload" />
</form>
and the following code to handle the uploaded file and supposedly put into MyPage
's album:
if(count($_FILES)){
$name = ereg_replace(' ', '_', basename($_FILES['fileSelect']['name']));
$uploadFile = "uploads/" . $name;
if (move_uploaded_file($_FILES['fileSelect']['tmp_name'], $uploadFile)) {
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($uploadFile);
$data = $facebook->api('/THE_ALBUM_ID/photos', 'post', $args);
//get rid of the original on the main server
unlink($uploadFile);
}
}
However, when the "Upload" button is submitted, I get this error, right in MyPage
's iframe for MyApp
:
Fatal error: Uncaught OAuthException: (#120) Invalid a开发者_Python百科lbum id thrown in /COMPLETE_PATH/facebook-php-sdk/src/base_facebook.php on line 1033
Even tho the album id is really correct, and displays when I use Graph API Explorer, or graph.facebook.com/THE_ALBUM_ID
By the way, I put this on top of the script:
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'THE_APP_ID',
'secret' => 'THE_APP_SECRET',
));
What am I missing? Any help would be much appreciated.
The only way I found to upload photos to a page through the Facebook API is doing it AS the Page. In order to do that you need to authenticate as the page first. To do that you have to:
- Authenticate with the user who owns the page with the
manage_pages
permission. Request the pages with
/me/accounts?access_token=YOUR_ACCESS_TOKEN
Find the
access_token
of the corresponding page.- Use that
access_token
to upload photos.
If you need USERS to upload photos to the page you could get a long lived token with the user who owns the page and store it to use that token instead of users tokens to upload photos. The uploaded photo won't appear uploaded as the user though, it will appear uploaded by the page.
You might try using CURL and hitting the graph api direct instead of using the SDK. The below works for me. I have a function that I pass in a variable called image which is an array of token,file(full path to file), and the album id and then use the following code.
$args = array(
'access_token' => $image['token'],
'message' => 'Photo Caption',
'no_story' => 1
);
$args['image'] = '@' . $image['file'];
$target_url = "https://graph.facebook.com/".$image['album_id']."/photos";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_exec($ch);
curl_close($ch);
You'll need to make sure you have the manage_pages
and the upload_photos
permissions granted to the token you pass into the call.
精彩评论