Operator Error

adventures in software development and continuous learning

Get a Facebook API Access Token That Never Expires

UPDATE (02/08/2013): Facebook has deprecated the offline_access token, so this method no longer works! See this post on Stack Overflow to learn how to extend an access token to 60 days.

I’ve been working on a few scripts that performed some minor data collection using the Facebook Graph API as a data source. Ideally, I wanted this script to be able to run several times a day automatically, however Facebook grants access tokens that expire after 1-2 hours. Since I’m the only one that is going to run this script, I only needed access to my Facebook data and I didn’t want to have to re-authenticate every few hours. Luckily, Facebook provides a permission parameter to get a “long-lived” access token from Oauth called offline_access!

NOTE: If you’re unfamiliar with the Facebook API authentication process, I suggest you brush up before continuing!

Below is an example of the permission parameter you would pass to the Oauth dialog to let Facebook know you don’t want some wimpy access token, but one that will last much longer!

https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=offline_access

Then just follow the rest of the Facebook authentication process normally and you should be set!

From the Facebook Permissions Reference:

offline_access – Enables your app to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.

However, if you’re feeling extra lazy, you could always generate an offline_access access token using the Graph API Explorer!

  • Head on over to the Graph API Explorer
  • Click the Get Access Token button
  • Select the Extended Permissions tab
  • Check the offline_access box
  • Click the Get Access Token button!

Facebook Graph API Explorer permissions

Now just grab your newly generated access token and get to work!

Comments