PHP - Post to Facebook Page using SDK no errors but no posts showing - facebook

I'm using the tutorial here http://www.adamboother.com/blog/automatically-posting-to-a-facebook-page-using-the-facebook-sdk-v5-for-php-facebook-api/ to post to a facebook page using the PHP sdk.
I have create the app on Facebook, and I have created the Never Expire Page Access Token, which I have verified using the Access Token Debug Tool https://developers.facebook.com/tools/debug/accesstoken/
However, when I run my test code, it does NOT return any errors, but I get no posts on my page. I get a response back from my print_r($graphNode) of the following
Facebook\GraphNodes\GraphNode Object
(
[items:protected] => Array
(
[id] => 1204777049583245_1264619360265680
)
)
The code for my test post is below, can anyone explain to me WHY I am not getting any posts to show up?
define("FACEBOOK_APP_ID","MY APP ID");
define("FACEBOOK_APP_SECRET","MY APP SECRET");
define("FACEBOOK_APP_TOKEN","MY PAGE ACCESS TOKEN");
define("FACEBOOK_SDK_V4_SRC_DIR",$_SERVER['DOCUMENT_ROOT'].'/socialmedia/Facebook/src/Facebook/');
require_once($_SERVER['DOCUMENT_ROOT'].'/socialmedia/Facebook/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v2.2',
]);
//Post property to Facebook
$linkData = [
'link' => 'www.yoururl.com',
'message' => 'test message'
];
$pageAccessToken = FACEBOOK_APP_TOKEN;
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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 '<pre>';
print_r($graphNode);
echo '</pre>';
exit;

Related

Cannot fetch E-Mail from Facebook SDK

Cannot fetch E-Mail address from facebook SDK for some particular E-Mail accounts. The facebook account I am testing with do have their E-Mail address in facebook. I am having this issue for some particular accounts only. Is there any settings which I have to activate from facebook. To fetch E-Mail I am using PHP Facebook SDK, the code is listed below:
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxx',
'default_graph_version' => 'v2.11',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?locale=en_US&fields=id,name,email', $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;
}
$user = $response->getGraphUser();
echo '<pre>';
var_dump($user);
exit;
If I use artdarek/oauth-4-laravel I can fetch the E-Mail address for the exceptional cases as well. But as the project is live I cannot Install the same in live server. Please suggest an alternative way out with PHP Facebook SDK.

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 GRAPH api list videos from page

I am trying to list all videos from a Facebook Fan Page an i got some problems. I keep getting Facebook SDK returned an error: You must provide an access token.. I tried using the access token, but no use.
CODE :
use Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
use FileUpload\FacebookFile;
use FileUpload\FacebookVideo;
use Facebook\Authentication\AccessToken;
$expires = time() + 60 * 60 * 2;
$accessToken = new Facebook\Authentication\AccessToken('mytoken1234567890', $expires);
var_dump($accessToken);
$fb = new Facebook\Facebook([
'app_id' => 'XXX',
'app_secret' => 'XXX',
'access_token' => "mytoken1234567890",
'default_graph_version' => 'v2.5',
]);
$request = $fb->request('POST', '/uniladmag/videos');
try
{
$response = $fb->getClient()->sendRequest($request);
}
catch(Facebook\Exceptions\FacebookResponseException $e)
{
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e)
{
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
print_r($graphNode);
?>
OUTPUT :
object(Facebook\Authentication\AccessToken)#2 (2) {
["value":protected]=>
string(17) "mytoken1234567890"
["expiresAt":protected]=>
object(DateTime)#3 (3) {
["date"]=>
string(26) "2017-02-14 18:20:13.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
}
Facebook SDK returned an error: You must provide an access token.
What solutions do i have? Do i miss something?
Thanks.

Facebook api autopost

i use facebook api for auto post from my website.
here is my code, after that i explain my problem:
require_once "Facebook/autoload.php";
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxx',
'app_secret' => 'xxxxxxxxx',
'default_graph_version' => 'v2.2'
]);
$pageAccessToken = 'xxxxxxxxxxxxxxxxxxxxx';
$linkData = [
'link' => $link,
'message' => $message,
'picture' => $pic
];
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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();
my problem is that, this code work sometimes, not all time.
and in sometime i see these errors:
Severity: Notice
Message: Undefined offset: 1
Filename: Http/GraphRawResponse.php
Line Number: 108
and
Severity: 4096
Message: Argument 4 passed to Facebook\FacebookResponse::__construct() must be of the type array, null given, called in /home/deponews/public_html/application/controllers/Facebook/FacebookClient.php on line 225 and defined
Filename: Facebook/FacebookResponse.php
Line Number: 75
i go to api explorer , and select my page from drop down and at the bottom drop down , select access page token , then select my page name again.
and copy generation token into my page access token.
i don't know why sometimes this code work well , and sometimes its not.
any idea ?
tnx

facebook graph error invalid signature

been wrestling with the facebook graph api a while now and am still not able to get what I want.
What am I missing? I have this code just as a test:
<?php
// );
$fb = new Facebook\Facebook([
'app_id' => '{313084575421679}',
'app_secret' => '{APP_SECRET}',
'default_graph_version' => 'v2.2',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '{APP_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;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
?>
it gives me this error Graph returned an error: Invalid OAuth access token signature.
later I want to retrieve al, the photo's links so I can create a collage of the pictures in my album.