Hello I created an API to post directly, it works very well on my facebook profile, the status is ok when I try to publish a status in my page, the post goes in the visitors publications what do I do ?
I looked in the Facebook Graph API, it would seem that this is a bug .. that you can bypass with using Curl..?
ps/ I edited the information page id, app id, secret app
Thanks in advance for your help
Stéphanie
public function statutPage(){
$fb = new Facebook([
'app_id' => 'my app id',
'app_secret' => 'my app secret',
'default_graph_version' => 'v2.8',
]);
$pageID='my page id,;
$token='A_VALID_USER_ACCESS_TOKEN';
$attachment = [
'access_token' => $token,
'message' => 'Premier message auto',
'name' => 'Première publication sur facebook',
'caption' => 'Legend sous le tire',
'link' => 'https://www.la-programmation.surleweb-france.fr',
'description' => 'Description du lien',
'picture' => 'https://www.google.fr/images/srpr/logo11w.png'
];
try {
$response = $fb->post('/'.$pageID.'/feed/', $attachment);
} catch(FacebookAuthorizationException $e) {
echo 'Graph retourne une erreur: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK retourne une erreur: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Posté su Facebook avec l\'id: ' . $graphNode['id'];
}
note apparanlty code below only works for "user profiles" and not for "pages". If you want info on how to handle pages, please look into the answer given at Facebook SDK v5 Post as Page on Wall!
But with that being said, Reading the Facebook PHP Developers Docs for publishing to a feed on Graph API 5.0, I see they're use the FacebookRequest object and then executing the method execute on it. It however returns a GraphObject - which seems deprecated in versions higher than 4.
They also mention to be sure to have publish_actions permissions on the account you're logged in with to auto post.
Reference PHP SDK code from facebook /feed/ SDK docs (in the url scroll down to 'publishing') for Graph API 5.0 - note that it returns a GraphObject - which seems deprecated in versions higher than 4. - I edited the example to use getGraphNode as referred to in https://developers.facebook.com/docs/php/FacebookRequest/5.0.0.
/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array (
'message' => 'This is a test message',
)
);
$response = $request->execute();
$graphNode = $response->getGraphNode();
/* handle the result */
Alternatively they also mention using the graph api directly by link to Publish with Graph API.
Related
I am struggling going round in circles getting an Facebook App to post to a Page's Wall, as that Page.
** Note: I put out a 300 point bounty to update an outdated answer of a related, well-viewed, existing question. Happy to award anyone who also answers there with the updated, working, solution
In brief:
I have the App Secret and ID
I have Page ID
I am relying on an in-code approach to generating the token I need.
It is based on a solution I saw posted on the SDK GitHub page but it does not work
I am using API v 2.9
I am using the latest PHP SDK
It's just been hard.
The solution I used is purely based on the one below:
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/Facebook');
require_once __DIR__ . '/Facebook/autoload.php';
use Facebook\FacebookRequest;
$app_id = {{your app-id}};
$app_secret = {{your app-secret}};
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => '{{your app-version}}',
]);
$token = $fb->get(
'/oauth/access_token?client_id=' . $app_id . '&'.
'client_secret=' . $app_secret . '&'.
'grant_type=client_credentials');
$get_token = $token -> getdecodedBody();
$access_token = $get_token['access_token'];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/{{your facebook name}}', $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;
}
$body = $response->getBody();
print_r($body);
?>
I am certainly not lazy. I have been stuck on this issue for weeks.
I have tried the following different solutions and have gotten stuck in either a permissions related issue that was also related to depracated functionality and API calls or getting the right token to do it.
Posting to a facebook page from a website using curl (using a pure CURL approach - "The user hasn't authorized the application to perform this action")
Facebook SDK returned an error: You must provide an access token. Php Facebook Api - (suggests that I support login and redirection callback - yet I do not want any user intervention everytime when posting onto the page)
Graph returned an error: Invalid appsecret_proof provided in the API argument - Previous attempt was based on this example but the appsecret-proof issue
Simple example to post to a Facebook fan page via PHP? - Another CURL-based approach that failed me
https://stackoverflow.com/a/14212125/919426 - Best explained step by
step process for permissions and tokens, but still failed
Update:
Tried the following and got the error "Invalid appsecret_proof provided in the API argument"
$fb = new Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.9',
]);
$postData = [
'message' => "Test Message",
'picture' => 'https://www.website.com/image.jpg'
];
try
{
// 'LONG_LIVED_PAGE_ACCESS_TOKEN' obtained by going to https://developers.facebook.com/tools/debug/accesstoken and clicking
// "Extend Access Token"
$response = $fb->post('/' . 'PAGE_ID' . '/feed', $postData,"LONG_LIVED_PAGE_ACCESS_TOKEN");
}
catch (FacebookResponseException $e)
{
var_dump("Facebook Response Exception occured: \n" . $e);
exit;
}
catch(FacebookSDKException $e)
{
var_dump("Generic Facebook SDK Exception occured: \n" . $e);
exit;
}
catch(Exception $e)
{
var_dump("Uknown Exception occured while attempting to call the Facebook SDK API: \n" . $e);
exit;
}
There are countless more that I have tried and failed.
I am working on a module, where requirement is When Admin creates any new post on website or blog, then it should get post on its related Facebook Page also.
I want something like this. But its not working in my case.
Here is code
// require Facebook PHP SDK
// see: https://developers.facebook.com/docs/php/gettingstarted/
require_once("php-graph-sdk-5.5/src/Facebook/facebook.php");
require_once("php-graph-sdk-5.5/src/Facebook/autoload.php");
// initialize Facebook class using your own Facebook App credentials
// see: https://developers.facebook.com/docs/php/gettingstarted/#install
$config = array();
$config['app_id'] = 'myappud';
$config['app_secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$fb = new \Facebook\Facebook($config);
// define your POST parameters (replace with your own values)
$params = array(
"access_token" => "myaccesstoek", // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
"message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook",
"link" => "http://www.pontikis.net/blog/auto_post_on_facebook_with_php",
"picture" => "http://i.imgur.com/lHkOsiH.png",
"name" => "How to Auto Post on Facebook with PHP",
"caption" => "www.pontikis.net",
"description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."
);
// post to Facebook
// see: https://developers.facebook.com/docs/reference/php/facebook-api/
try {
$ret = $fb->post('myfburl/feed', $params);
echo 'Successfully posted to Facebook';
}
catch(Exception $e) {
echo $e->getMessage();
}
It giving error as
An unknown error occurred
For more refer this link => http://www.pontikis.net/blog/auto_post_on_facebook_with_php
Help to figure it out where I m making mistake.
Updated :
<?php
// Include facebook class (make sure your path is correct)
use Facebook\Facebook;
require_once ("php-graph-sdk-5.5/src/Facebook/autoload.php");
require_once("php-graph-sdk-5.5/src/Facebook/Facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'app_id' => 'myappid',
'app_secret' => 'myappsecret',
'cookie' => true,
));
//$token is the access token from the URL above
$post = array('access_token' =>'myaccesstoken', 'message' => 'new test post - ' . date('Y-m-d'));
try{
$res = $facebook->post('myfburl/feed',$post);
print_r($res);
} catch (Exception $e){
echo $e->getMessage();
}
for posting to a page you would need a page token with the manage_pages and publish_pages permissions
Example :
https://www.facebook.com/dialog/oauth?client_id=yourClientId&redirect_uri=yourUri/&scope=manage_pages,publish_pages
I am using php-sdk-4 for facebook wall posting using my app credential. Everything is working fine. Problem is only posting on user facebook wall or through user post on his facebook fan page wall working but when I used his facebook fan page secret code for publish a post on facebook fan wall then show me error :-
(#200) The user hasn't authorized the application to perform this action
My facebook app in testing mode
I am doing all these steps as admin.
In my project I want functionality user come login through facebook and select his facebook fan page and publish some text or image on his facebook fan page
Code :-
$facebook = new Facebook(array(
'app_id'=>'XXXXXXXXXX',
'app_secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5'
));
$graph_url_pages = "https://graph.facebook.com/me/accounts?access_token=".$accessToken;
$pages = json_decode(file_get_contents($graph_url_pages)); // get all pages information from above url.
for($i=0;$i<count($pages->data);$i++)
{
$page_token = $pages->data[$i]->access_token;
$pageId = $pages->data[$i]->id;
$params = array(
'access_token' => $page_token,
'from' => 'XXXXXXXXXXX',
'to' => $pageId,
'message' => 'Google Inc',
'link' => 'http://www.google.com/',
);
try {
$response = $facebook->post('/'.$pageId.'/feed', $params);
} 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();
print_r($graphNode);die;
}
In code when I passed access_token then show me error but when I passed $accessToken (user access token) then user post going on facebook fan page wall but I want facebook fan page publish a post on timeline.
I've created a Facebook App using the latest version of the PHP SDK (version 5) which publishes posts to my Facebook page when ran.
This is working...however the posts are going onto the page as 'private', so only I can see the posts when logged in to my personal Facebook page. the public cannot see them.
Does anyone know why this may be? Am I right in assuming I need to 'submit the App to Facebook for review' before it can send public posts to my page?
Here's my code below:
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/facebook-sdk-v5/');
require_once(__DIR__.'/src/facebook-sdk-v5/autoload.php');
//Set your Facebook API settings
$fb = new Facebook\Facebook([
'app_id' => 'apptoken',
'app_secret' => 'appsecrettoken',
'default_graph_version' => 'v2.2',
]);
//Post property to Facebook
$linkData = [
'link' => 'http://www.google.co.uk',
'message' => 'Test message'
];
try {
$response = $fb->post('/me/feed', $linkData, 'thepageaccesstokenhere');
} 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();
One solution is to make the app public. By default, I believe it's in development mode.
To do this, please follow below steps:
1. Go to https://developers.facebook.com
2. From the left sidebar, select "App Review"
3. Set "Make your app public?" to "Yes"
Page post made by your app should be available even if you're not logged in.
You are Facebook app & page admin. That's why you are able to post as page, but it's for test purposes only so hidden from other users. You have to apply for permission (post_as_page) to achieve this.
I'm developing with Laravel a package to post on facebook. My code responsible for obtaining and storing the access token of the page post a link besides test makes this operation correctly with an app that I have created test fb . The problem is to create another app on facebook to put it into production , this second app facebook created it with the same configuration that does work but when publishing gives me the following error: (# 200 ) The user hasn 't Authorized the application to perform this action .
This is the code snippet I use for testing .
public function getTest(){
//$accessToken = new AccessToken($this->getParam('TOKEN'));
try {
$page_post = (new FacebookRequest($this->session, 'POST', '/'.$this->getParam('PAGE_ID').'/feed', array(
'access_token' => $this->getParam('TOKEN'),
'link' => 'link',
'description' => 'Hola mundo desde laravel',
'picture' => 'link/img.png',
'message' => 'Messge',
) ))->execute()->getGraphObject()->asArray();
// return post_id
print_r( $page_post );
} catch(FacebookSDKException $e) {
var_dump($this->getParam('TOKEN'));
echo $e->getMessage();
// var_dump($e);
exit;
}
As for the authorization of the application is correctly even when you enter to view user applications see 2 ( The works and which not) , both with the same permissions accepted.
Fixed , I lacked a permit : ' publish_pages '
$params = array(
'scope' => 'manage_pages','publish_actions','publish_stream','publish_pages'
);