Posting a photo as an open graph facebook page
I have an application that has many open graph pages. I'm able to post as these pages just fine, except for photos. I get a "PHP Fatal error: Uncaught OAuthException: A user access token is required to request this resource." which makes no sense because I can post to the feed as the page no problem.
Heres the code:
require_once ('facebook.php'); $fb = new Facebook(array( 'appId' => 'XXX', 'secret' => 'XXX', )); $fb->setFileUploadSupport(true); $args = array('message' => 'Photo Caption'); $args['image'] = '@' . realpath("testpic.jpg"); $data = $fb->api("/PAGEID/photos", "post", $args); print_r($data); ?>
Is this possible to do without needing a user to login? I read that you can request the page access_token as a user, but it seems kind of dumb that you can use one way of authenticating for one function, then another way for another. I'd like to avoid an user logins if possible.
The app posting the photo needs to include in $args an access token for admin'ing the page. This is also called a "page access token" or "page token".
$args['access_token'] = $access_token;
The way I get such an access token is to obtain manage_pages,publish_stream permissions from the user (me) who is an admin of the page. Then:
$page_info = $facebook->api('/'.$PAGE_ID.'?fields=access_token');
$access_token = $page_info['access_token'];
See also: Developer blog post