Reputation: 3940
I feel like I've been all over the facebook documentation as well as hours of google searching and I cannot figure this out. Much of what I found is years old; before Facebook switched to another API version so I'm getting confused about a ton of things.
It seems my business pages already have apps created for them, so all I'm trying to do is get the feed from the page. Here is my code so far, and I'd just like to know what to do next.
$facebookAPP_ID = 'THERE IS A VALID ID';
$facebookAPP_SECRET = 'THERE IS A VALID SECRET';
require 'facebook/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication($facebookAPP_ID, $facebookAPP_SECRET);
$session = FacebookSession::newAppSession($facebookAPP_ID, $facebookAPP_SECRET);
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
echo $ex->getMessage();
}
Doing $session->getToken()
retrieves a token which is cool but I want data.
The following code directly after gives an error. An active access token must be used to query information about the current user.:
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
$object = $response->getGraphObject();
echo $object->getProperty('name');
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
I've read that there is no '/me' for pages, so what is the correct call to get the page feed?
Upvotes: 1
Views: 7834
Reputation: 3940
It seems Facebook just whispers this part in its documentation; that you can authenticate using only the ID and Secret, so this one call over cURL is working for me without any of that code above being necessary (with the correct values of course).
'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
And I'm able to get the Facebook ID here.
Upvotes: 4
Reputation: 74014
The correct API call for a page feed would be /page-id/feed. You can also use /page-id/posts
or some other endpoints, as you can read in the docs.
This works fine with an App Access Token for non-restricted Pages. If it´s restricted by age or country, you need a Page or User Token.
Upvotes: 1