Background
I’m having a bit of trouble finding out just how to programmatically post photos to a Facebook Page. I’ve read through the Facebook Documentation on Authenticating as a Page, but all the examples involve Facebook Users. So far I’ve just generated the needed access tokens manually using Facebook’s Graph API Explorer. (3)
What I’ve tried
Using this simple code, I was successful in uploading the picture, however it was posted on my own wall, in my own album under my own name, not the on the Facebook Page, in the Facebook Page‘s album under the Facebook Page‘s name. I need to post it on a specific Facebook Page as the Facebook Page itself. (1)
<?php
require 'facebook-php-sdk/src/facebook.php';
require 'config.php';
$facebook = new Facebook(array(
'appId' => $config['facebook_appId'],
'secret' => $config['facebook_appSecret'],
'fileUpload' => true
));
$facebook->setAccessToken($config['facebook_pageAccessToken']);
$args = array(
'message' => 'Facebook API test',
'source' => '@'.realpath(__DIR__.'/404.jpg')
);
$data = $facebook->api('/'.$config['facebook_pageId'].'/photos', 'post', $args);
About the code
$config['facebook_appId']is from a dummy test app I created$config['facebook_appSecret']is from a dummy test app I created$config['facebook_pageAccessToken']is generated from the Graph API Explorer using the scopemanage_pages,publish_stream,photo_uploadandoffline_access(3)- As a side note to potential future visitors, it turns out I generated a User Access Token instead of a Page Access Token, which is the reason it was posted on my own wall, in my own album as myself.
$config['facebook_pageId']is the ID of the page (which I also found using the Graph API Explorer)- Upon successful completion
$datacontains the ID of the image and the wall post(?) it seems
Question summary
- How can I post photos to a Facebook Page as the Facebook Page itself?
- This is for a project where developers are coming and going. Is there a way for me to have an immutable Page Access Token independent of a specific Facebook User OR dependent on a (dummy) Facebook App OR generating one on the fly somehow?
- How can I (if necessary) authenticate as a Facebook Page – giving me a Page Access Token, programmatically in PHP?
Code examples or links to concise documentation and/or tutorials preferably with examples will be much appreciated.
The answer to all three is:
Get the user, who is an admin of the page, to grant your app
manage_pagespermissionIf you did this client-side (i.e Javascript SDK), you’ll want to extend their access token using the instructions for Scenario 4 on https://developers.facebook.com/roadmap/offline-access-removal/
Retrieve the
access_tokenfor the page via a call to/me/permissionsor a call to/[PAGE_ID]/?fields=access_token. If you extended the page admin’s access_token before retrieving the page access token, the page access token will not expire unless the user stops being an admin of the page or changes their password, revokes permission to the app, etc (Scenario 5 on the doc above)Use that access token to upload the photo to the page’s wall.
I think most of these steps are clarified here: https://developers.facebook.com/docs/authentication/pages/ (logging in as the page) or in this the Page documentation