How To: Facebook Access Token

I’m a big fan of the Facebook Graph API. It’s easy to use and well documented. Recently, the facebook feeds on a few of our client sites went down. Upon investigation, it appears that Facebook has changed the way data can be accessed through their API. Feed data can now only be retrieved using an authentication token. The process was relatively simple to set up, but not immediately straightforward.

Here’s what you need to do in order to get an authentication token:

  1. Go to http://www.facebook.com/developers, login to your facebook account, and add the developer app, if requested.
  2. Click the ‘add a new app’ button to create a new app.
  3. Give the app a name, keeping in mind facebook’s naming policies.
  4. After creating the app, you should end up at an admin panel with your new app information ready.  Go to ‘https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APP_ID&client_secret=APP_SECRET’ replacing APP_ID with your newly created app ID and APP_SECRET with your new app Secret, both of which are displayed on the application administration panel.  If the credentials are correct, you will see your authentication token at that location.
  5. This token can now be used in order to retrieve data from a feed.  For example: ‘https://www.facebook.com/PAGE_OR_USER_ACCOUNT/feed?access_token=ACCESS_TOKEN’, replace PAGE_OR_USER_ACCOUNT with the page id or user account name and ACCESS_TOKEN with the token you retrieved in step 4.

It’s that easy!

A few things to note:

  • Your access token will change periodically, implying your Facebook data retrieval method will need to dynamically retrieve the access code from Facebook using the method in step 4.
  • Facebook Apps can be url specific, or you can leave those fields blank and use this authentication method to retrieve public data from Facebook from multiple urls.

Visit www.glassfest.org to see how we recently implemented a Facebook feed into a client’s site design.

In: Tutorials

Spread the Word:
  • Facebook
  • Twitter
  • Slashdot
  • Digg
  • Google Bookmarks
  • NewsVine
  • StumbleUpon
  • RSS
8 comments »

Comments

  1. Ahmad

    May 4, 2012 at 5:35 pm

    Thanks for this tutorial

  2. David

    May 17, 2012 at 3:40 am

    Thanks very much for this – it would have taken me a very long time to figure it out on my own.

    Do you have any suggestions for how I could go about retrieving the access token dynamically?

    D

    • Kai

      May 18, 2012 at 10:02 am

      We’ve typically used curl or file_get_contents with php using the address in step 4. Querying the https address will return null on error (last time I checked) or the access token on success, so it’s easy to check if everything worked on the fly.

      • David

        May 18, 2012 at 10:15 am

        Excellent – thanks; I’ll check those out.

  3. Tatiana Cooper

    July 6, 2012 at 11:24 pm

    Thanks, it is explained well and looks easy.

  4. sk

    July 26, 2012 at 10:09 am

    I followed the instructions and got my access_token but when i try to use it on one of my own public photo albums:

    https://graph.facebook.com/album_id/photos?access_token=token here]

    i get an empty data set, completely stumped :/

  5. Geegee

    July 31, 2012 at 4:27 am

    Excellent, found what I was looking for

Leave a Comment

Your email address will not be published.

For your safety, comments are moderated before appearing on this page.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>