I'm trying to fetch Facebook public video detail like the video title and thumbnail.
Here is my code
<?php
session_start();
require 'Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v11.0',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get(
'/{video_id}',
'{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;
}
echo $graphNode = $response->getGraphNode();
?>
the above code is returning this error
Graph returned an error: (#10) This endpoint requires the 'pages_read_engagement' permission or the 'Page Public Content Access' feature.
What does it mean?
Using this example from fb-sdk:
require_once 'fb-sdk/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123456',
'app_secret' => 'abc1233',
'default_graph_version' => 'v2.6',
]);
$data = [
'message' => 'Testupload',
'source' => $fb->fileToUpload('image.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/PageID/photos', $data, 'XXXXXXYYYYYY');
} 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 'Photo ID: ' . $graphNode['id'];
i am able to post as myself to the wall of my facebook-page.
'app_id' and 'app_secret' come from the app i created for this.
If i set $response = $fb->post('/me/photos', $data, 'XXXXXXYYYYYY'); the post shows up on my timeline.
What do i need to change, so get a photo, posted by "Page" on the feed of "Page"?
You need to use a Page Token with the publish_pages permission to post "as Page". You can get a Page Token with the following API call, after authorizing with manage_pages and publish_pages:
/page-id?fields=access_token
More information:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
when I publish a post to my page, it's working fine, but when I post to page through set page ID instead of "me",
the response as the following:
Graph returned an error: (#200) Permissions error
the source code as the following:
require_once 'src/Facebook/autoload.php';
//*
$config = array();
$config['appId'] = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_id'] = $config['appId'];
$config['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_secret'] = $config['secret'];
define('APP_ID', $config['appId']);
define('APP_SECRET', $config['secret']);
//$config['fileUpload'] = false; // optional
$config['default_graph_version'] = 'v2.5';
$config['page_id'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$config['facebook']['permissions'] = array(
'email',
'user_location',
'user_birthday',
'publish_actions',
'publish_pages',
'manage_pages',
'public_profile',
);
$fb = new Facebook\Facebook($config);
if(isset($_SESSION['fb_access_token'])) {
$accessToken = $_SESSION['fb_access_token'];
} else {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
if(isset($accessToken)) {
$oAuth2Client = $fb->getOAuth2Client();
// longlived access token
if (!$accessToken->isLongLived()) {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
}
}
} 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)) {
// Logged in!
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/'.$config['page_id'].'/feed', $accessToken);
} 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;
}
} else {
$helper = $fb->getRedirectLoginHelper();
$redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$loginUrl = $helper->getLoginUrl($redirect_url, [ 'manage_pages', 'publish_pages', 'publish_actions' ]);
echo 'Log in with Facebook!';
}
on other action in same page
$page_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// instance
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => encodeurl(array('page'=>'page','lang'=>'ar','id'=>$_id,'beg'=>0,'unuse'=>$xtitlex)),
'message' => $xtitlex,
'source' => $publish,//$fb->fileToUpload($publish),
'caption' => DOMAIN_,
"picture" => $publish,
"name" => $xtitlex,
"description" => $xdescriptionx
];
try {
//
$response = $fb->post('/'.$config['page_id'].'/feed', $linkData, $page_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
//print_r($linkData);
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
Most likely, you are not using a Page Token with the correct permissions. Make sure you are using a Page Token instead of a User Token and make sure it includes publish_pages. You can test your Token in the debugger: https://developers.facebook.com/tools/debug/
Make sure the Page ID shows up, and the publish_pages permission.
First check if the Access Token you used is generated as "Page Access Token" not "User Token". It is a bit tricky and took me a moment.
go to "https://developers.facebook.com/tools/explorer/"
drop down "User Token" menu
pick "Page Access Token"
blue button "Generate Access Token"
copy and use new token
Every time you add or remove Permissions, token changes!
Screenshot
My app types was business and 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
I don't why I am getting this error.
My appid and secret id are all correct. Don't know what went wrong.
<?PHP
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload('C:/xampp/htdocs/laravel5-learning/public/image/share/'.$name),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $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();
echo 'Photo ID: ' . $graphNode['id'];
?>
It is showing this error:
Graph returned an error: Invalid OAuth access token.
I've searched all over for this, the 'answers' I got were outdated. Anytime I run the script it posts the message with my profile to the facebook page even though I have admin permissions.
This is the code I'm using
<?
require_once __DIR__ . '/Facebook/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4'
]);
$page_id="";
$accessToken='Access token I got from https://developers.facebook.com/tools/explorer/145634995501895/';
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken('');
try {
$response = $fb->get('/me');
$userNode = $response->getGraphPage();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
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;
}
echo 'Logged in as ' . $userNode->getName();
?>