Graph API Facebook SDK for PHP Permissions Error - facebook

I am working on Auto Posting on facebook page or group by using graph api. But i get error of permissions but i allow permissions But i face this error. Any one who know about this error.
Code given below.
I share some images Which error showing me or how to allow permissions.
<?php
require_once ('vendor/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.10',
]);
$pageAccessToken = '{page-access-token}';
$MsgData = [
'message' => 'Hi, My name is Zaheer'
];
try {
$response = $fb->post('/me/feed/' , $MsgData , $pageAccessToken );
}
catch ( \Facebook\Exceptions\FacebookResponseException $e ) {
echo 'Graph returned an error ' . $e->getMessage();
exit;
}
$GraphNode = $response->getGraphNode();
echo 'ID :' . $GraphNode['id'];
?>
[![enter image description here][2]][2]

Debug your Token: https://developers.facebook.com/tools/debug/accesstoken
You can see that it is a User Token, not a Page Token. Page Tokens can be generated by using a User Token with the following endpoints:
/me/accounts?fields=access_token
/page-id?fields=access_token

Make sure to use page token. You can easily get one from the the graphi api explorer on the right panel. I was having permissions error. My app type was business and to remove it, I just added all the permissions from the given list and after I added all permissions, facebook asked me to review permissions, I simply kept clicking on OK and everything worked, the error went :D

Related

How to get images from FB to website

I'm working on publishing posts from our FB page on our website. Each new post and photos we added into our FB page I need to show in our website too.
I have this code, but don't working as I need.
require 'php-graph-sdk-5.x/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '...',
'app_secret' => '{app-secret}', // don't know where to find app_secret for my page, I've found that just for apps, not pages
'default_graph_version' => 'v2.10'
]);
$access_token = '...'; // generated in developers, but with just for 12 hours, than this token is expired... :-(
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('me?fields=id,name,feed,data', $access_token);
} 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;
}
$graphNode = $response->getGraphNode();
foreach ($graphNode as $node) {
foreach ($node as $items) {
print_r($items);
// when access token was valid, I got posts from out FB page. But with no images, didn't find how to get add images to this feed...
}
}
So, my questions are:
where I can find app-secret for FB page? I'm not able to connect to my FB page without that, I think
how to generate permanent access token, or if it's not possible, how to make i other way?
how to add images (new, shared, etc.) to my feed?
Thanks.
There is no App Secret per Page, there is only the App Secret per App. You can find it in your App Settings.
Extended/Permanent Tokens: https://developers.facebook.com/docs/facebook-login/access-tokens/ or http://www.devils-heaven.com/facebook-access-tokens/ - for getting the feed entries of unrestricted Page, you can just use an App Access Token and you can hardcode it in your PHP code.
Getting images in the feed, not just messages: You need to specify which fields you want to get, or you will only get default ones. It is called "Declarative Fields": https://developers.facebook.com/docs/graph-api/changelog#v2_4 - also, check out the API reference: https://developers.facebook.com/docs/graph-api/reference/post

Do I need permission to programatically post on my wall?

I want to programatically post a new story on a page I own. Do I need to ask Facebook for permissions to do that? When I invoke Facebook -> post I get an error: (#200) The user hasn't authorized the application to perform this action.
I wanted to request the publish_actions permission, but it seems to be permission for my app to post to app users' walls, which is not what I intend to do, I just want to publish on my own wall.
This is my code:
$fb = new Facebook\Facebook([
'app_id' => 'my app id',
'app_secret' => 'my app secret',
'default_graph_version' => 'v2.9',
'default_access_token' => 'the access token'
]);
$linkData = [
'link' => 'http://example.com/link/for/today',
'message' => 'This is the link for today',
];
try {
$response = $fb->post('/me/feed', $linkData);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
Thanks for any help!
To post to a page as the page, you will need permissions manage_pages and publish_pages, and you will need to get a page access token first ... and then use that to create the post.
https://developers.facebook.com/docs/facebook-login/access-tokens/#pagetokens

Facebook app evaluate the 'Stream post URL security' flag from another facebook app

I have two facebook apps 'A' and 'B' associated with domain_A and domain_B respectively; 'A' has activated the 'Stream post URL security' flag.
I need to post content to fanpage X using "facebook sdk 4.0 for php" from app 'B', the problem is the post only success disabling A's Stream post URL security flag; otherway FacebookRequest throws an exception:
One or more of the given URLs is not allowed by the Stream post URL
security app setting. It must match the Website URL or Canvas URL, or
the domain must be a subdomain of one of the App's domains.
It makes no sense, why facebook api checks this flag in app A?
I have double check the ids and secret keys, maybe A has an open session permanently like facebook users in navigators?
This is the code:
FacebookSession::setDefaultApplication($api_id_B,api_secret_key_B);
FacebookSession::enableAppSecretProof(false);
$session = FacebookSession::newAppSession($api_id_B,api_secret_key_B);
try {
$post_id = (new FacebookRequest(
$session,
'POST',
'/' . $fanpage_id_X . '/feed',
array(
'access_token' => $fanpage_token_X,
'message' => $message_post,
'link' => $link_inside_domain_B,
'caption' => $caption_post,
'name' => $link_inside_domain_B,
'description' => $description_post,
'published' => true )
)
)->execute()->getGraphObject()->asArray();
echo 'post shared!';
} catch (FacebookRequestException $e) {
echo 'ERROR! ' . $e->getMessage();
} catch (Exception $e) {
echo 'ERROR! ' . $e->getMessage();
}
Any help will be appreciated,
thanks.
#WizKid was right, the real problem turned out that the $fanpage_token_X was actually generated for A, so its policies were applied, even when B initiated the session.

FaceBook PHP API SDK: suddenly the argument 'link' is not published and /me/feed is not working

I completed this script, that was working fine till this morning, when launching it, suddenly I noticed that the argument "link" of the array $linkData, is not published any longer, (no error returned) while "message" is published.
I also noticed that if I put /me/feed into the object $fb->post, it doesn't work (see the commented line). No errors returned.
While, if I put the code of a group (the code you see is a test group I created to test the script), the argument "message" is published (not the "link")
The scopes are:
user_managed_groups,
user_events,
user_friends,
user_posts,
publish_actions,
user_actions.music,
user_actions.video,
public_profile
The link is referred to the link for which the application is written. the www.example.com URL, is written here for this example. In the real code, I use the URL of the site of the App (that till yesterday was working fine).
Please here the code:
<?php
session_start();
define(__APP_ID__, XXXXXXXXXXXXXXX);
define(__APP_SECRET__, 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY');
define(__APP_TOKEN__, 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ');
define(__APP_ACCESS_TOKEN__, 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW');
require_once '../include/setup.php';
require_once $root_dir.'/include/Facebook/autoload.php';
// Creates the object
$fb = new Facebook\Facebook([
'app_id' => __APP_ID__,
'app_secret' => __APP_SECRET__,
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'The link above, is not any longer published',
];
$group_id = "1041863359178529";
try {
//$response = $fb->post("/me/feed", $linkData, __APP_ACCESS_TOKEN__); // SUDDENLY it doesn't work any longer
$response = $fb->post("/{$group_id}/feed", $linkData, __APP_ACCESS_TOKEN__);
} 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;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id']."\r\n";
?>
EDIT:
Is there anything I can do? Is id depending from the App settings? I changed many parameters to find a solution but I still have the issue.
I did verified that the block, is due FB.
In other words: probably of security reasons (mainly Anti-Spam), FB block repetitive use of the function in short time.
I did try also to wait more than 15 seconds among the iterations, but if I employ it for more than 20-25 time in one day, FB suspends all the required authorizations.
At the time I did post the thread, FB was enabling only the post of the text, and deactivated OpenGraph.
Now, instead, it blocks any kind of sending, including text.

Facebook status update code fails to update wall

I got the sample code from Facebook to post a message. At first it worked fine. The next day I tried to clean up the code and it stopped working. I brought back the sample code from Facebook, to take out all my changes, and it still did not work.
I put in print statements to follow the code.
$user_id is false, so it displayes a login in. The login in no longer goes to a login in screen. It calls my return url, which then sees $user_id as being false and diplays the login in again.
I did notice when my call back was being called, I had the following values:
v?state=452c9492a3d51962909f5e000bcb0965
and code
code=AQDucDgsdc3lZFVN2MC3Oj2oB0n1LT4FjOrG3MbgwL4uhh--LS-mRdtjU-6oSUZxsR6UhTKuUDVn3hrasJAe5r1I6ksIDJz1nnTo1mjCqEInJKvQ2qK5A-N3_Nt5buGLqsirb8ccg21N4nWVWkk_iRePwX9f68qK1j-2O6E_USpKvRJeNP3bcwLBiTBJpDVu7aA#=
I was wondering maybe when I was trying to clean up the code and kept running it, Facebook flagged my app as spamming? I posted 26 posts to my wall - is that too many? And how would I check if my app has been flagged as a spammy app?
This is the sample code I used
<?
// 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' => 'myappid',
'secret' => 'mysecret',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
print ("tryinmg to do a poast");
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
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.
print ("face book exception log in");
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
echo '<br />logout';
} else {
print ("no user login in do login");
// 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' ) );
echo 'Please login.';
}
?>
</body>
</html>
Naturally, Facebook has a limit for the number of post per day your app can make to a user wall. And FB have policy for user to report spammy apps as well.
The exact maximum number of posts depends on your application FB ranking. In my experience, 26 posts on a user wall is near the limit for new application.
If that is the case, you should receive the error message from Facebook, which says something like "you have reached limit of posts..."
P/s: It seems you need time to refactoring your question, though. If I have not met this situation before, I won't understand your question.