8

I'm using the Facebook PHP SDK to call relevant APIs to post and get data. At present I'm saving a user access token in the database but it expires after 60 days. How can I refresh a user access token?

1 . When do I need to refresh the access tokens? After it expires, or before?

2 . What is the best way to refresh access tokens?

3 . Should my users need to login again to refresh access token?

This is the function that I'm using to extend the access token. but expire time remain same.

public function getExtendedAccessToken($access_token)
    {       

        $token_url="https://graph.facebook.com/oauth/access_token";
        $params=array('client_id'=>self :: appId,'client_secret'=>self :: appSecretId,'grant_type'=>'fb_exchange_token','fb_exchange_token'=>$access_token);

          $response = $this->curl($token_url,$params);                  
          $response = explode ('=',$response);
          $response = explode ('&',$response[1]);
          $response = $response[0];       
          return $response;

    }
1
  • Yes, the user has to visit your app again to get a new access token.
    – CBroe
    Jul 26, 2013 at 15:04

3 Answers 3

6

The access token can't be refreshed just like that, the user should access the app again to get the new token.

You can refresh the token at anytime you wish. In fact, the best way is to refresh the token each time the user visits your app. In this way, the token will never be expired if the user keep visiting the app once in 60 days.

9
  • Thanks for the reply. The issues is user may visit my app more than 100 times in a day. than it will be refresh by 100 times. is that ok?? and how can i refresh access token? Jul 27, 2013 at 6:00
  • Well, refreshing 100 times a day would probably be overkill, why not do it at most once per day? the access token refresh is clearly documented, pasting the endpoint parameters here would just be redundant: developers.facebook.com/docs/facebook-login/access-tokens/…
    – Igy
    Jul 27, 2013 at 6:30
  • Yes I've implement this technique in my project but it is not exceeding the time. see the below point in that link. it mentions that you need to refresh the access token and that is my question how? Jul 27, 2013 at 9:00
  • Try the same tomorrow, u'll see the time extended. Jul 27, 2013 at 11:35
  • @sahil I've tried it and nothing happens see here are below two access token before extending and after extending. Before Extending :"CAAFpZAbXad7MBAHZCnN9haieepdpwDp70jAR16xoI63ETeDt5FZAVUtSkmPCF0SKRq62IeBpZASztZA2UiE0Ilw200Dq89Bx0ZAz2MtLBswnOq0kYcXHdnwVhuk5vljfrHt2ul99C1MH6UuMACk0B2N1FMkZAT5aMsZD". After Extending :"CAAFpZAbXad7MBAOvdyzOZBZCjbv2xE97C5gWt0vWKmr5HSYPJLqFl7hnbcBrR768ug6hg6Pndt1DDMEqrZAgcjLQea3blboYyB3iAX1xKU7I0CCmUy1uxtl9L6KwxXXRPMWHKxgMoK6DcgcWIwPYPtv1Mue7gj4ZD".. Jul 30, 2013 at 5:54
0

Try this:

$app_id = FB_APP_ID;
$app_secret = FB_SECRET_ID;
$canvas_URL = FB_PAGE_URL;
$code = $_REQUEST["code"];

if(empty($code)) {
  $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
  $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . $canvas_URL . "&state="
    . $_SESSION['state'];

  echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$facebook->api('oauth/access_token', array(
    'client_id'     => FB_APP_ID,
    'client_secret' => FB_SECRET_ID,
    'type'          => 'client_cred',
    'code'          => $code,
));
$token = $facebook->getAccessToken();
echo$token;
}
0

public function getExtendedAccessToken($access_token) {

    $token_url="https://graph.facebook.com/oauth/access_token";
    $params=array('client_id'=>self :: appId,'client_secret'=>self :: appSecretId,'grant_type'=>'fb_exchange_token','fb_exchange_token'=>$access_token);

      $response = $this->curl($token_url,$params);                  
      $response = explode ('=',$response);
      $response = explode ('&',$response[1]);
      $response = $response[0];       
      return $response;

}
2
  • 1
    Please add an explanation along with the code, so that it can help others. Jul 19, 2017 at 15:01
  • per the FB documentation, fb_exchange_token will only exchange short-lived client tokens to server-side long-lived tokens. Are you sure that this will also refresh/extend long-lived tokens?
    – atyachin
    May 2, 2019 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.