Home > Backend Development > PHP Tutorial > How Can I Extend Facebook Access Token Validity After Offline_Access Deprecation?

How Can I Extend Facebook Access Token Validity After Offline_Access Deprecation?

Patricia Arquette
Release: 2024-11-26 06:33:10
Original
851 people have browsed it

How Can I Extend Facebook Access Token Validity After Offline_Access Deprecation?

Extending Access Token Validity Post Offline_Access Deprecation

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

Note:

  • Ensure you have enabled "Deprecate Offline Access" in your Advanced App Settings.
  • Call setExtendedAccessToken to obtain the extended token.
  • Access the extended token using getAccessToken.

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template