← Back to blog home

Get Facebook Access Token for Graph API

07 Jun

Facebook recently updated their Graph API to require an oAuth access token. Here is a quick guide that explains the easiest way to create an access token for your site.

 

Create a Facebook App

First you need to create a facebook app that you own / control. To do this go to this url: https://www.facebook.com/developers/ and click "Set Up New App". Enter a name for the app and agree to the terms. It doesn't matter what you name the app. Once that is done go to the Web Site tab on the left and enter the URL for your site in the Site URL box. This is required or you will get an invalid referral error when you try to generate your token.

Once you have your app created copy and paste the "Application ID" we will need this later.

Generating the token

Now we need to build the URL to create our access token. There are 3 important parts to this process.

client_id – This is your Application Id you copied earlier

redirect_uri – this should be the URL to your site

scope – this is what you want the access code to allow access to. If you don't specify "offline_access" the access code will expire when you log out of facebook.

https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=####&redirect_uri=http%3A%2F%2Fitslennysfault.com&scope=read_stream,offline_access

Now you just have to replace the "####" with your application id and "itslennysfault.com" with your website's url and paste it in to your browser's URL bar. You will then be taken to the typical facebook allow permissions page. Once you click "Allow" you will be redirected to your web site with some extra data.

The URL when you are redirected back should look like this:
http://itslennysfault.com/#access_token=######&expires_in=0

Where the ##### is is your access token… copy and paste that and you're good to go. You can use this access code to Get latest status from facebook fan pages in PHP.

 
 

Tags: , ,

Leave a Reply

Notify me of future comments

(I don't spam or share your e-mail. Unsubscribing is as easy as clicking the "unsubscribe" link in the notifications)
 

 

 
  1. Garrett

    June 9, 2011 at 4:43 pm

    Thank you Thank you Thank you,
    This has relieved quite an aggressive headache.
    Was very frustrated with Facebook’s OAuth switch.

     
  2. Euan

    June 18, 2011 at 12:15 am

    What a great help, thanks for posting this. It’s a pain when fb keeps changing stuff but good peeps like yourself save the day and it’s much appreciated. Cheers!

     
  3. Romil Dhakar

    February 10, 2012 at 2:51 pm

    Thanks for the help. Much appreciated. Wish FB could publish your post on their official forum.

     
  4. Adam Cleaner

    March 30, 2012 at 7:27 pm

    worked nice and easy without any fuss. thanks

     
  5. Max Payne

    April 4, 2012 at 6:42 am

    Hi thanks for your Beautiful tutorial.

     
  6. Dan H√łegh

    July 30, 2012 at 7:45 am

    From: http://developers.facebook.com/docs/authentication/

    Please note: On October 3, 2012, the offline_access permission will be removed. If you are building a new application you shouldn’t use this permission. Instead please check the Deprecation of Offline Access Permission document which explains how to start using access tokens that are valid for up to 60 days.

     
  7. Jake Bellacera

    September 27, 2012 at 9:52 pm

    This post is a little out of date as Facebook recently changed their API.

    Once you create your app, you’re going to want to go to Apps > Your App > Advanced and then set “Remove offline_access permission” to “disabled” as that will allow you to retain offline_access.

     
  8. kumar gaurav

    April 3, 2013 at 8:43 am

    offline_access is removed now from facebook api and the access tokens get expired after logging out if created this way..
    Any idea how to do the same now??

     
  9. Raj

    April 10, 2013 at 3:27 am

    Excellent Tutorial :)

     
  10. Ian

    May 9, 2013 at 2:36 pm

    It seems that FB has made yet another change and is depreciating the ‘offline_access’ piece, forcing the access token to expire after ~90 minutes. Any advice would be much appreciated.

     
  11. https://www.facebook.com /mazlan.otai.14/

    February 25, 2014 at 12:29 am

    Hye nak auTo Like. . . . . .kLik hare

     
  12. logicum

    March 11, 2014 at 1:36 am

    this step by step guide will get you a permanent access token http://logicum.co/getting-a-facebook-page-permanent-access-token/