Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get wall feed from a public Facebook page using Graph API - is it really this complex?

I have started off by reading Displaying Facebook posts to non-Facebook users which is of some use but I cannot believe it is this difficult to get a public feed from Facebook.

The page I want a feed from is public, you do not need to be logged into get to it.

Am I right in presuming that I need an access_token to get to this information, attempting to access the URL without results in an OAuth error.

So the flow should be like this (massively, overly complex):

  1. Authenticate using a user (what if the user isn't on Facebook?)
  2. Some complex OAuth nonsense - just to read the feed, I do not even want a like button or post to wall functionality
  3. Get the feed using a PHP request to the correct URL with the user's access_token
  4. Render the feed

Assuming the user isn't on Facebook, what do you do, use a generic app to get the feed?

  1. Hardcode an auth request to Facebook using my generic app's ID and secret
  2. Some complex OAuth nonsense
  3. Get the feed using a PHP request to the correct URL with the app's access_token
  4. Render the feed
  5. Oh no, the auth has expired, re-auth and capture this new access_token for use in future requests.

This seems really complex for no reason other than Facebook wants to know EVERYTHING that is going on, it'd be easier to do a cURL and scrape the content from the public URL using XPath.

Any help on this would be great.

Thanks, Jake

EDIT

An edit to show this is not an exact duplicate.

I had this working with an access_token in place, but now it fails, the token has expired and I can no longer use it to obtain information from the public wall.

I attempted to extend the expiration date of this token using the methods mentioned in other posts but this didn't work and the expiration was not extended - we are now here, with an invalid token and no further along.

It seems that the manual process of having to approve the OAuth request means that it is impossible to programatically get the feed of a public page.

like image 545
Jake N Avatar asked Mar 18 '12 15:03

Jake N


1 Answers

Two years later, you can programmatically do this with a Facebook App (an example using PHP and Slim): https://developers.facebook.com/apps/

$base_api="https://graph.facebook.com/";
$client_id="XXXXXX";
$app_secret="XXXXXX";

//get a profile feed (can be from a page, user, event, group)
$app->get('/feed/:profileid/since/:start_date', function ($profile_id,$start_date) {

    $start_time=date('m/d/Y h:i:s',$start_date);

    $request = new FacebookRequest(
      getSession(),
      'GET',
      '/'.$profile_id.'/feed?since='.$start_time
    );

    $response = $request->execute();
    $graphObject = $response->getGraphObject();

    //do something with $graphObject

});


function getSession(){
    $session = new FacebookSession(getAccessToken());
    return $session;
}


function getAccessToken(){
    global $base_api, $client_id, $app_secret;
    $url=$base_api."oauth/access_token?client_id=".$client_id."&client_secret=".$app_secret."&grant_type=client_credentials";
    $str = file_get_contents($url);
    $token = str_replace ( "access_token=" , "" , $str );
    return $token;
}
like image 197
wblaschko Avatar answered Oct 14 '22 18:10

wblaschko