How To: Post On Facebook Page As Page Not As Admin User Using PHP-SDK
Posting as Admin is easy but posting as Page requires more work from your side. To post as Page we need the following permissions:
publish_streammanage_pages
Using the page object
UPDATE: Facebook has extended their
pageobject and now you can retrieve the pageaccess_tokendirectly from the page. The code below uses the latest methods.
UPDATE #2: the below example was broken because for some reason if the admin didn’t grant the app themanage_pagespermission, the/$page_id?fields=access_tokencall is returning the page id instead of an error. And hence, a permission check was added as a work around and a bug was reported.
We need the manage_pages permission to get the page access token so we can post on its behalf. And this is how we do it:
<?php
// This code is just a snippet of the example.php script
// from the PHP-SDK <https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php>
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = 'page_id';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => "I'm a Page!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
} else {
$permissions = $facebook->api("/me/permissions");
if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
!array_key_exists('manage_pages', $permissions['data'][0])) {
// We don't have one of the permissions
// Alert the admin or ask for the permission!
header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
}
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
// ... rest of your code
?>And the result:
Using the Javascript-SDK
Achieving the same with only client-side technology:
function postToPage() {
var page_id = '195234433832970';
FB.api('/' + page_id, {fields: 'access_token'}, function(resp) {
if(resp.access_token) {
FB.api('/' + page_id + '/feed',
'post',
{ message: "I'm a Page!", access_token: resp.access_token }
,function(response) {
console.log(response);
});
}
});
}Back to topUsing /me/accounts connection
If you are building some sort of a CMS where the admin wants to select one of the pages to update its status, then using the /me/accounts is better you can list the pages and save their access_token in one API call.
$page_id = "XXXXXXXXX";
$page_access_token = "";
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
if( !empty($page_access_token) ) {
$args = array(
'access_token' => $page_access_token,
'message' => "I'm a Page!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
// sucess...
} else {
// couldn't find the page!
}Always communicate with your page fans as Page not as Admin, fans do know (and trust) your page (product) but not necessary you!
Back to top- http://cv.zerkms.com zerkms
- http://cv.zerkms.com zerkms
- http://www.masteringapi.com Ibrahim Faour
- PK
- http://www.masteringapi.com Ibrahim Faour
- http://www.rncreatives.com Ricky Diancin
- http://www.masteringapi.com Ibrahim Faour
- http://www.rncreatives.com Ricky Diancin
- http://www.masteringapi.com Ibrahim Faour
- http://www.rncreatives.com Ricky Diancin
- Codersam
- Thasnias
- http://www.facebook.com/raufozgen Rauf Özgen
- http://www.masteringapi.com Ibrahim Faour
- Antonio Baptista
- Anonymous
- AJ Quick
- frf
- frf
- maxleb
- Oosswwaalldd
- Gruese
- yyy
- Gerwin
- Mail2jatingarg
- Apathetic012
- http://www.artwerkstadt.de/ Michael
- http://profile.yahoo.com/OZLN2MRV5LASX57XVWJ22Z2BMM s
- http://twitter.com/IM_adventure Vampire Weekend
- Debashis
- http://www.masteringapi.com Ibrahim Faour
- jay123
- manoj
- http://twitter.com/poktipop mcbjam
Table of contents
Advertisment
Recent Tutorials
- How To: Create A User Photo Albums Browser Using Facebook Graph API
- How To: Upload A Photo To A User’s Profile Using Facebook Graph API
- How To: Check Status And RSVP To Facebook Events Using Graph API & FQL
- Facebook Javascript SDK Best Practices
- How To: Create Facebook Events Using Graph API – Advanced







