Home >Backend Development >PHP Tutorial >How Can I Obtain Long-Lived Facebook Access Tokens After Offline_access Deprecation?

How Can I Obtain Long-Lived Facebook Access Tokens After Offline_access Deprecation?

DDD
DDDOriginal
2024-12-01 17:11:14423browse

How Can I Obtain Long-Lived Facebook Access Tokens After Offline_access Deprecation?

Long-Lived Access Tokens After Offline_access Deprecation

Facebook's deprecation of the offline_access permission has hindered the acquisition of long-lived access tokens. Despite documentation claiming server-side OAuth tokens to be extended, the reality is different.

Solution

To obtain a long-lived access token, utilize the extendedAccessToken () method:

public function getExtendedAccessToken(){

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'), 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;
}

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'];

}

Remember to enable "deprecate offline_access" in the Advanced settings of the Developer App to ensure functionality. Call this method after receiving the regular access token to obtain a valid token with a 60-day expiration period.

The above is the detailed content of How Can I Obtain Long-Lived Facebook Access Tokens After Offline_access Deprecation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn