How do I get Permission to post to Facebook Page Wall - facebook-graph-api

After reading the developers.facebook.com for the past few hours I just can't seem to figure out how to get my Facebook Page to allow permission for me to post to its wall from a Django Website?

you must give related permissions to your app(publish_stream,manage pages)
then you can post to page as page
posting to page as page if you have page's access_token can be like this:
include_once ('src/facebook.php');/// include sdk
////// config The sdk
# $facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXXXXXXXX',
));
try{
$post=$facebook->api('PAGE_ID/feed/','POST',array(
'message' => '$message',
'link'=>'http://apps.facebook.com/xxxxxxx/link.php?link=',
'picture'=> 'XXXXXXXXXX',
'name'=>'XXXXXX',
'description'=>'yes',
'access_token'=>'PAGE ACCESS TOKEN'
));
}
catch(FacebookApiException $e) {
echo $e->getType();
echo '<br />';
echo $e->getMessage();
}
& if you dont have access token (you need above permissions again) i think this can help you
include_once ('src/facebook.php');/// include sdk
////// config The sdk
# $facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXXXXXXXX',
));
$facebook->destroySession();
try{
$post=$facebook->api('PAGE ID/feed/','POST',array(
'message' => '$message',
'link'=>'http://apps.facebook.com/xxxxxxx/link.php?link=',
'picture'=> 'XXXXXXXXXX',
'name'=>'XXXXXX',
'description'=>'yes',
));
}
catch(FacebookApiException $e) {
echo $e->getType();
echo '<br />';
echo $e->getMessage();
}
to learn how to get page's access_token check this link up:
https://developers.facebook.com/tools/explorer/?method=GET&path=me%2Faccounts

Related

Error when extracting comments in my facebook page through laravel app using Facebook graph API

I'm trying to extract comments in my facebook page. For that I made a Facebook graph app. I can extract the comments in Graph API explorer using {page-id}?fields=posts.limit(1000){comments} query.
But when I tried to get comments through a laravel app, it gives me an error. The code in laravel app is
$fb = new Facebook([
'app_id' => getenv('FACEBOOK_APP_ID'),
'app_secret' => getenv('FACEBOOK_APP_SECRET'),
'access_token' => getenv('FACEBOOK_ACCESS_TOKEN'),
'default_graph_version' => 'v3.3',
]);
$accessToken=getenv('FACEBOOK_ACCESS_TOKEN');
try {
$response = $fb->get('/{page-id}?fields=posts.limit(1000){comments}',$accessToken);
} catch(Facebook\Exceptions\FacebookResponseExcepti9on $e) {
$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// echo 'Facebook SDK returned an error: ' . $e->getMessage();
$e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
Error is
(#10) To use 'Page Public Content Access', your use of this endpoint must be reviewed and approved by Facebook. To submit this 'Page Public Content Access' feature for review please read our documentation on reviewable features: https://developers.facebook.com/docs/apps/review.
This is how my graph permission and features page looks. What am I doing wrong?

Facebook Graph API SDK v5 - Publishing a picture to a page's Album

I'm trying to publish a picture to an album belonging to a Facebook page, using the Facebook PHP SDK v5.
I've already checked the (many) similar questions, however they are all related to prior versions of the SDK, when publish_actions was still allowed.
My Setup:
My app is properly configured and I was able to retrieve my access token
I was able to run a few test API queries with no issues
I'm the admin of this page and my access token has the following permissions ['manage_pages','publish_pages','user_photos']
The SDK was properly initialized with 'fileUpload' => true as a parameter
Code publishing the picture to the album
It is based on the example provided in the SDK documentation.
<?php
require_once __DIR__.'/vendor/autoload.php';
$fb = new Facebook\Facebook(['app_id' => '123456', 'app_secret' => '123456', 'default_graph_version' => 'v3.3', 'fileUpload' => true]);
$picture = 'https://www.example.com/pic.jpg';
$album_id = '123456';
$access_token = 'mytoken';
try {
$response = $fb->post('/'.$album_id.'/photos', array ('url' => $picture), $access_token);
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
die('Graph returned an error: ' . $e->getMessage());
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
die('Facebook SDK returned an error: ' . $e->getMessage());
}
$graphNode = $response->getGraphNode();
Here's the error I'm getting
Graph returned an error: (#200) This endpoint is deprecated since the
required permission publish_actions is deprecated
Indeed, changes introduced in April 2018 removed the ability to use publish_actions however it seems the documentation has not been updated accordingly.
What is the new way to publish a picture to a page's album?
Your help is much appreciated!
After several hours of researching alternate solutions, I was able to find a workaround.
Apparently I was using an User Access Token instead of the Page Access Token.
Solution
1 - First, verify if your token is a User/Page Access token
2 - If it is an User Token, you need to request the Page Access Token instead:
$user_access_token = 123456;
$page_id = 123456; // Can be retrieved via right click on your page logo + Copy link address
$response = $fb->get('/'.$page_id.'?fields=access_token', $user_access_token);
$page_access_token = json_decode($response->getBody())->access_token;
echo $page_access_token;
3 - Finally, post the picture on the Page using the Page Access Token
Full code
<?php
require_once __DIR__.'/vendor/autoload.php';
$fb = new Facebook\Facebook(['app_id' => '123456', 'app_secret' => '123456', 'default_graph_version' => 'v3.3', 'fileUpload' => true]);
$picture = 'https://www.example.com/pic.jpg';
$album_id = '123456';
$user_access_token = 'mytoken';
$page_id = 123456;
try {
$response = $fb->get('/'.$page_id.'?fields=access_token', $user_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
die('Graph returned an error: ' . $e->getMessage());
} catch(Facebook\Exceptions\FacebookSDKException $e) {
die('Facebook SDK returned an error: ' . $e->getMessage());
}
$page_access_token = json_decode($response->getBody())->access_token;
try {
$response = $fb->post('/'.$album_id.'/photos', array ('url' => $picture), $page_access_token);
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
die('Graph returned an error: ' . $e->getMessage());
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
die('Facebook SDK returned an error: ' . $e->getMessage());
}
$graphNode = $response->getGraphNode();
I hope this helps!

"The domain of this URL isn't included in the app's domains" when it is

I'm trying to post to a Facebook wall from a PHP script. I've created a FB app, installed the Graph PHP API, and implemented some test scripts as follows:
fbinit.php:
<?php
session_start();
require_once('src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => 'REDACTED',
'app_secret' => 'REDACTED',
'default_graph_version' => 'v2.9',
]);
?>
fbpost.php:
<?php
include('fbinit.php');
$helper = $fb->getRedirectLoginHelper();
$permissions = ['manage_pages','publish_pages']; //'publish_actions'
$loginUrl = $helper->getLoginUrl('https://www.REDACTED.net/fb-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
fb-callback.php:
<?php
include('fbinit.php');
$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
exit;
}
try {
$response = $fb->get('me/accounts', $accessToken->getValue());
$response = $response->getDecodedBody();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo "<pre>";
print_r($response);
echo "</pre>";
?>
The first time I opened fbpost.php, I was asked to log in to Facebook as expected, and it asked for permission to post on my behalf on the page, which is fine. But then I am redirected to the call back page and presented with the following error:
Graph returned an error: Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
I have added every combination of the app URL's and callback URL's I can think of, but nothing works. See below for screenshots of the app settings. The App ID and secret are definitely correct.
The value of the redirect_uri parameter needs to be the exact same in your login dialog call, and the subsequent API call that tries to exchange the code for a token.
When you have generation of the login URL and handling of the response spread over different scripts (i.e., called via different URLs), that can easily lead to problems like this. The SDK tries to figure out the value based on the current script URL, if left to its own devices.
In such a case, explicitly specify the callback URL in your getAccessToken method call as well.

adding like comment issue using graph api

i have put last 3 posts in web page , if user loged in he can add comment / like , problem is if user loged in and try add like or comment he is getting an permission error #200 , if i loged in i can add like or comment (application is for me) , am getting the all permission nedded from the user , so how can i give him permission to add like / comment.
CODE :
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
if (session_id()) {
} else {
session_start();
}
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions', 'GET', array(
'access_token' => $access_token
)
);
//check if the permissions we need have been allowed by the user
//if not then redirect them again to facebook's permissions page
$permissions_needed = array('publish_stream', 'read_stream', 'manage_pages');
foreach ($permissions_needed as $perm) {
if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) {
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri' => 'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
}else {
//if not, let's redirect to the ALLOW page so we can get access
//Create a login URL using the Facebook library's getLoginUrl() method
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri'=>'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
exit();
}
$logoutUrl = $facebook->getLogoutUrl();
and if the user click on like button :
jQuery('ul.fb_list a').click(function(){
var comm_id = jQuery(this).attr("class");
jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
access_token : "<?php echo $access_token ?>"
});
});
Double check and make sure your variable comm_id is formated with both the user id of the person who posted the post, and then the id of the post itself, with an underscore in between, like this - USERID_POSTID. This is how facebook gives it you if you call to the graph api
$post_url = "https://graph.facebook.com/" . $user_id . "/posts?access_token=". urlencode($access_token);
Not sure if you're getting comm_id from another source or not. Also noticed in your code
access_token : "<?php echo $access_token ?>"
You forgot a semi-colon after the echo call. Should be this
access_token : "<?php echo $access_token; ?>"
Hope this helps. I see you're using a lot of jquery to do the like functionality. I like to stick w/ server side code for stuff like this, I feel that it's more stable for some reason.
This is how I did it. I have a like button like this -
echo '<div id="like_container-'.$i.'">
<div id="like_count">'.$num_likes.'</div>'
<div class="unliked"></div>
</div>';
and fb_Like() is an ajax call, something like this -
function fb_Like(post_id, token, num_likes, id){
$.ajax({
type: "GET",
url: "likepost.php",
data: 'id='+post_id+'&token='+token+'&likes='+num_likes,
success: function(html)
{
$("#like_container-"+id).empty().html(html);
}
});
}
And the likepost.php page is a script similar to the one on this page
Like a Facebook Post externally using Graph Api - example
This worked really well for me. It also let's me update the number of likes that the post has on the front end right above the like button if a like has been made. Good luck!
UPDATE
If you want to check if the user already likes a post, it's pretty simple w/ the facebook graph api
//Create Post Url
$post_url = "https://graph.facebook.com/" . $Page/User_id . "/posts?access_token=". urlencode($access_token);
//Get Json Contents
$resp = file_get_contents($post_url,0,null,null);
//Store Post Entries as Array
$the_posts = json_decode($resp, true);
foreach ($the_posts['data'] as $postdata) {
foreach ($postdata['likes']['data'] as $like){
if($like['id']==$user){
$liked=1;
}else{continue;}
}
if($liked==1){
//do something
}
}
This assumes that you already have a facebook user id for the logged in user, in this example, stored in the variable $user.

I want to post from my site on my fanpage

I have create an app in facebook and, between php sdk, i can post the article of my site on my profile.
Now, i want to post my article in my fanpage and not in my prfile. how i can do this?
This is the code that i use, not much different from the example in facebook developers:
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('facebook.php');
$config = array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$access_token = $facebook->getAccessToken();
$page_id='xxxxxxxxxxxxxx';
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => $link,
'message' => $titolo,
'picture' => 'http://xxxxxxxxxxxxxxxxxx',
'name' => 'Ecosport',
'access_token' => $access_token
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
header("location:$login_url");
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
$id_articolo=$_GET['id_articolo'];
header("location:xxxxxxxxxx");
/*echo '<br />logout';*/
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
header("location:$login_url");
/*echo 'Please login.';*/
}
?>
Oviusly the "xxxx" are used for privacy. I tied to change this " $facebook->api('/me/feed',...... " into this " $facebook->api('/$page_id/feed',....... " but nothing. I can't to post my article in my fanpage.
Can you help me?
Thank you very much.
You need to ask for the manage_pages and publish_stream permissions. Then call to /me/accounts to get a list of accounts (aka pages) that the user administers. Each account in that list of accounts will have an access token associated with it. That access token is special and will need to be used to new up an instance of the facebook api. Once you do that, posting to the page wall is the same as posting to a user's wall (i.e. /me/feed)