Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Getting a Long-Lived Access Token

Facebook API: Getting a Long-Lived Access Token

7

The latest version of the Facebook PHP SDK makes it easy to get a long-lived access_token, giving you access to the user’s profile for up to 60 days, opposed to 1-2 hours with the regular access_token. The long-lived access_token can be used to perform tasks on the user’s behalf for long periods of time, for example, accessing check-in data for up to 60 days.

The setExtendedAccessToken() function exchanges the user’s short-lived access token for a long-lived one in just a few short lines of code:

The long-lived access_token means the user doesn’t need to login to your application for an extended period of time. In addition, using the long-lived access token with the manage_pages permission gives you access to page tokens that do not expire.

Note:

Just like short-lived access token, the long-lived token expires if the user changes their password, deauthorizes an app, or logs out. In these cases, the app should handle errors gracefully.

Facebook, Facebook Graph API, PHP

7 comments on “Facebook API: Getting a Long-Lived Access Token”

  1. hakangokerhakan says:
    March 17, 2014 at 4:24 AM

    thats not what its gonna do. if you will see the facebook api the $facebook->setExtendedAccessToken(); will get it from session. you stupid you just copy paste code here without trying

    Reply
    • Niraj Shah says:
      March 17, 2014 at 11:35 AM

      Did you even look at the code? This is what it looks like. Please tell me where it’s getting it from the session. Check your facts before you make a fool of yourself.

      public function setExtendedAccessToken() {
          try {
            // need to circumvent json_decode by calling _oauthRequest
            // directly, since response isn't JSON format.
            $access_token_response = $this->_oauthRequest(
              $this->getUrl('graph', '/oauth/access_token'),
              $params = array(
                'client_id' => $this->getAppId(),
                'client_secret' => $this->getAppSecret(),
                'grant_type' => 'fb_exchange_token',
                'fb_exchange_token' => $this->getAccessToken(),
              )
            );
          }
          catch (FacebookApiException $e) {
            // most likely that user very recently revoked authorization.
            // In any event, we don't have an access token, so say so.
            return false;
          }
      
      Reply
  2. soulstitchmmo says:
    May 23, 2014 at 7:29 PM

    Hello Niraj,

    I’m trying to use this in Google Spreadsheets, and using email addresses to get UID. How would I set up sheet to take an email address and pull it’s UID out?

    Reply
    • Niraj Shah says:
      May 24, 2014 at 5:41 PM

      Why are you using Google Spreadsheets for this? You should be using a proper database like MySQL or even Parse to store and retrieve information.

      Reply
      • soulstitchmmo says:
        May 26, 2014 at 7:47 PM

        Because I am learning how to interact with Facebook API at the moment still learning, don’t know where to start outside of what I know.

  3. Pratik Jagtap says:
    August 13, 2015 at 11:24 AM

    Hi, Niraj,

    I have facebook page URL in my mysql database.I want to fetch each page reviews and store it in database using cron job.Please help.Post code related to this.I am still stuck in getting page access token.

    Reply
    • Niraj Shah says:
      August 14, 2015 at 7:53 PM

      Sorry, I don’t post code for specific problems unless you’re tried something yourself. Please post your code and question on StackOverflow.

      Reply

Leave a Reply to Pratik JagtapCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy