How to get Facebook Access Tokens in PHP for public posts – The Basic Sauce

So you want an access_token for the Facebook Graph API in the simplest way possible?

Getting public (fan) page posts

  • Create a Facebook application
  • Then go to https://developers.facebook.com/tools/access_token

  • Take the second token, for public posts, any access_token will do so let’s take the app token.

App access tokens can be generated for Facebook Apps. Generate app access tokens with a different login flow than the other tokens; this can be done in code.

This type of token is useful to modify app settings, create and manage test users or read App Insights data. You can use app tokens to publish or delete content on behalf of a user who gave your app permissions. For example, if a user granted your app publish_stream permissions, your app can use an app access token to publish a status update on their behalf.

App tokens do not expire unless the App Secret is reset. App access tokens are unique to each app.

The login flow is not needed if you don’t reset the application secret. Thus you have an access_token with no expiration and can bypass browser interaction.

  • Now we can make requests by hardcoding (yes, Facebook says don’t but if you don’t reset the secret then it’s okay @_@, see previous point)
$access_token = '1111111111|2Cha_1-n5'
    $graph_url = "https://graph.facebook.com/Boo/posts?access_token=" 
        . $access_token;
    $page_posts = json_decode(file_get_contents($graph_url), true);

Then you can do like one of the previous posts – Getting Your Facebook Page On Your Website With Access Tokens

foreach($page_posts['data'] as $post){
        $post_link = $post['actions'][0]['link'];
        $page_id = $post['from']['id'];
        $page_name = $post['from']['name'];
        $message = ($post['message']) ? $post['message'] : " ";
        $name = ($post['name']) ? $post['name'] : " ";
        $story = ($post['story']) ? $post['story'] : " ";
        $post_time = $post['updated_time'];
    }

Just like… magic