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
Release: 2024-12-01 17:11:14
Original
422 people have browsed it

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'];
Copy after login

}

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template