0

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.

alpham8
  • 1,314
  • 2
  • 14
  • 32

1 Answers1

0

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.
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • Shouldn't 'access_token' => $this->sAccessToken be 'access_token' => $user->sAccessToken – Tobi Mar 13 '14 at 11:10
  • So what does your `setAccessToken` method look like? – CBroe Mar 13 '14 at 11:25
  • `setAccessToken` is just a simple setter for `$this->sAccessToken`. – alpham8 Mar 13 '14 at 12:20
  • So, what about my remark concerning 'access_token' => $user->sAccessToken I think that this might be the key – Tobi Mar 13 '14 at 12:23
  • I´m sorry. For the snippet above I forgot to replace `$pageData` with `$user`. I´ ve edited it. So, `$user->sAccessToken` is the same as `$this->accessToken` (as I set it in my last snippet). – alpham8 Mar 13 '14 at 12:23
  • Yes, but the error message indicates that there's a problem with the access token. You could try to log the token at the specific code piece, and run it through https://developers.facebook.com/tools/debug/accesstoken/ to see what the actual permissions of this token are – Tobi Mar 13 '14 at 12:40
  • I fixed the mistake of using `$pageData' instead of `$user` in my example above, but not in my code. So, I tried to submit something without setting an access token. This couldnot work. I wonder why it works on my own timeline. – alpham8 Mar 13 '14 at 13:23
  • It's still not correct in your second code example, but glad it worked out for you – Tobi Mar 13 '14 at 13:27