I would like to post a photo to a fan page (as a normal timeline status feed) which I am able to manage.
My app got the following permissions:
email,publish_actions,read_stream,manage_pages
It works perfectly for my own timeline. I am also able to post a photo on the page´s timeline. But why not from the app?
I also figered out, that the permission photo_upload will not take any effect. Maybe Facebook has removed it.
My app uses the Facebook PHP-SDK.
If I try to upload a photo to a fan page it returns me an exception:
Fatal error: Uncaught OAuthException: (#200) Subject does not have permission to post photos on this page thrown
Here is how I get the access token from the fan page:
public function getPageData()
{
$arRS = $this->facebook->api('/me/accounts');
$pageData = new CFacebookClientPageData();
foreach ($arRS['data'] as $dataSet)
{
if (array_key_exists('name', $dataSet) && preg_match('/any\s*fan\s*page/i', $dataSet['name']))
{
$pageData->sCategory = $dataSet['category'];
$pageData->sName = $dataSet['name'];
$pageData->sAccessToken = $dataSet['access_token'];
$pageData->arPermissions = $dataSet['perms'];
$pageData->iPageId = $dataSet['id'];
}
}
return $pageData;
}
And here is how I try to post it:
public function uploadPhoto(CFacebookClientPageData $user, CFacebookPhotoData $photo)
{
return $this->facebook->api('/' . $user->iPageId . '/photos',
'POST',
array(
'source' => '@' . realpath($photo->sLocalPath),
'message' => $photo->sMessage,
'access_token' => $this->sAccessToken
)
);
}
In some other file I call it like this:
$fb = new CFacebookClient($sAppId, $sSecret);
$fb->init();
$fb->setFileUpload(true);
$fb->init();
$user = $fb->getPageData();
$fb->setAccessToken($user->sAccessToken);
$photo = new CFacebookPhotoData();
$photo->sLocalPath = 'E:\php\www\logo.png';
$photo->sMimeType = 'image/png';
$photo->sMessage = utf8_encode('posted from my fresh app :-)');
var_dump($fb->uploadPhoto($user, $photo));
Edit: Solved as mentioned in my comment below: I fixed it in the snippet but not in my code.
I guess you're not using the Page access token you can receive for your app for this page via
/me/accounts
The docs at https://developers.facebook.com/docs/graph-api/reference/page/feed/#publish state that
- A user access token with publish_actions permission can be used to publish new posts on behalf of that person.
- A page access token with publish_actions permission can be used to publish new posts on behalf of that page.