Hi I need to post message on facebook fan page
this is my code
When I run this code I have following error
(#200) The user hasn't authorized the application to perform this action
if access_token token problem ?
how can i create access_token
<?php
require 'facebook-php-sdk-master//src/facebook.php';
$appId = '1617691071796143';
$secret = 'd84420ccfe2fa7eecac50ca96936bb21';
$returnurl = 'lankabird.com';
$permissions = 'manage_pages, publish_stream, offline_access,read_stream';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
$page_id = "1596941017191630";
$page_access_token = "";
//$page_access_token = "1617691071796143|d84420ccfe2fa7eecac50ca96936bb21";
$result = $fb->api("/me/accounts");
// loop trough all your pages and find the right one
if( !empty($result['data']) )
{
foreach($result["data"] as $page)
{
if($page["id"] == $page_id)
{
$page_access_token = $page["access_token"];
break;
}
}
}
else
{
echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
}
$fb->setAccessToken($page_access_token);
// Now try to post on page's wall
try{
$message = array(
'message' => "YOUR MESSAGE",
);
$result = $fb->api('/'.$page_id.'/feed','POST',$message);
if($result)
{
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
}
?>
Best Answer
publish_stream
andoffline_access
are deprecated since years, where do you guys keep copying your code? You needpublish_actions
(and of coursemanage_pages
in your case) to post on the wall, as you can read in the docs: https://developers.facebook.com/docs/graph-api/reference/v2.2/page/feed#publishAlso, keep in mind that you need to go through a review process with those permissions if you want to make your App public: https://developers.facebook.com/docs/apps/review/login
About the
read_stream
permission:Source: https://developers.facebook.com/docs/facebook-login/permissions/v2.2#reference-read_stream
...meaning, you will not get
read_stream
approved.Edit: You can also use the permission
publish_pages
now, if you want to publish something "as Page". See the changelog for more information: https://developers.facebook.com/docs/apps/changelog#v2_3_changes