how to refresh facebook access token for server side (in facebook-php SDK)? how to refresh facebook access token for server side (in facebook-php SDK)? php php

how to refresh facebook access token for server side (in facebook-php SDK)?


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.


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;}


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;}