Reputation: 61
I a trying to write a script which allows users to tag themselves on photos displayed on my website.
Once the user has selcted the people to be tagged the photo is uploaded to the Facebook Fan Page of my website, but facebook will not allow my app to tag the photo with the currently logged in users profile_id or that of any of their friends.
Everything is working great appart from adding the tags. I get the error: Fatal error: Uncaught OAuthException: An unknown error has occurred. thrown in /home/public_html/include/api/facebook.3.0.1/base_facebook.php on line 1039
Here is the code so far:
<?php
require_once ($_SERVER['DOCUMENT_ROOT'] . "/include/api/facebook.3.0.1/facebook.php");
//Name of album you want to create
$album_name = "December 4, 2011";
// Create our application instance
// (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID_INSERT_HERE',
'secret' => 'APP_SECRET_INSERT_HERE',
));
$facebook->setFileUploadSupport(true);
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
// Get access token for Facebook Page
$page_id = '170367249680012';
$page_info = $facebook->api("/$page_id?fields=access_token");
if (!empty($page_info['access_token'])) {
$param = array(
'method' => 'fql.query',
'query' => 'SELECT object_id FROM album WHERE owner="FACEBOOK_PAGE_ID_INSERT_HERE" AND name="' . $album_name . '"'
);
//Check if album exists
$album = $facebook->api($param);
if (isset($album[0]['object_id'])) {
// If album exists store id in the variable $album_uid
$album_uid = $album[0]['object_id'];
} else {
// ELSE Create album
$album_details = array(
'access_token' => $page_info['access_token'],
'message' => 'Album desc',
'name' => $album_name
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
}
//Photo Details
$file = 'test.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
//Upload a photo to album
$args = array(
'access_token' => $page_info['access_token'],
'image' => '@' . realpath($file),
'message' => "test"
);
$upload_photo = $facebook->api('/' . $album_uid . '/photos', 'post', $args);
//Get id of photo uploaded
$photo_id = $upload_photo['id'];
echo "<br/>" . $photo_id . "<br/>";
//ADD TAGS
$argstag = array('to' => $user);
$argstag['x'] = 40;
$argstag['y'] = 40;
$datatag = $facebook->api('/' . $photo_id . '/tags', 'post', $argstag);
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'status_update,publish_stream,user_photos,offline_access,manage_pages'));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
</body>
</html>
The interesting thing is if I use the same script to upload the image to the users profile and tag the image it works!
for those of you who want to do this remove the following line of code it appears twice 'access_token' => $page_info['access_token'],
And change $upload_photo = $facebook->api('/' . $album_uid . '/photos', 'post', $args);
To $upload_photo = $facebook->api('/me/photos', 'POST', $args);
I have searched high and low to see if there is a restriction stopping the API from allowing users to tag themselves or friends on Fan Pages but I can't find anything. The only thing I could find was this:
http://forum.developers.facebook.net/viewtopic.php?id=101485
So I am not sure if it is offically a bug, but I seem to be having having the same problem as they are. It was filed as a bug over 6months ago an there seems to have been no progress.
Does anyone know a workaround for this problem? I don't want to have to upload the image to the users profile and tag it.
Upvotes: 3
Views: 2174
Reputation: 15457
Check your Page settings. There is a setting that allows fans to tag themselves in photos. Make sure its enabled and try again. Look for the Tagging Ability
setting under Manage Permissions
.
Upvotes: 1