Problem:
With the deprecation of the offline_access permission in Facebook's Authentication flow, retrieving long-lived access tokens without that permission has become a challenge. Despite Facebook's documentation stating that server-side OAuth tokens will be long-lived, they are not.
Solution:
Facebook has updated the PHP SDK to include a method for extending access token validity.
Updated SDK Method:
A public function named setExtendedAccessToken has been added to base_facebook.php. This function returns a new access token with an expiration of 60 days.
Usage:
Call this function after receiving the normal access token. You can access the new token using the getAccessToken public function.
Extended Access Token Function:
public function getExtendedAccessToken(){ try { $access_token_response = $this->_oauthRequest( $this->getUrl('graph', '/oauth/access_token'), array( 'client_id' => $this->getAppId(), 'client_secret' => $this->getAppSecret(), 'grant_type' => 'fb_exchange_token', 'fb_exchange_token' => $this->getAccessToken() ) ); } catch (FacebookApiException $e) { return false; } if (empty($access_token_response)) { return false; } $response_params = array(); parse_str($access_token_response, $response_params); if (!isset($response_params['access_token'])) { return false; } return $response_params['access_token']; }
Note:
The above is the detailed content of How Can I Extend Facebook Access Token Validity After Offline_Access Deprecation?. For more information, please follow other related articles on the PHP Chinese website!