Facebook Graph API v2.3 Compatibility Issues
As the end-of-life date for Graph API v2.2 approaches, developers are encouraged to upgrade to v2.3. However, many have encountered unexpected issues while making the switch.
Missing API Responses
Requests such as the following, used to fetch album data, no longer return any results:
https://graph.facebook.com/v2.3/{$user_id}?date_format=U&fields=albums.order(reverse_chronological).limit(100).offset(0){id,count,name,created_time}
Loss of User Profile Information
Calls to the user endpoint no longer include the "birthday" field:
https://graph.facebook.com/v2.3/{$user_id}
Solution
The root cause of these issues lies with Facebook SDK version 3.2.2, which is compatible with PHP version 5.3. A significant change in v2.3's OAuth Access Token format requires the SDK to parse the response as JSON, instead of the URL-encoded format used in earlier versions.
SDK Code Update
To address this issue, update the getAccessTokenFromCode function in the SDK to parse the response as JSON:
$response = json_decode($access_token_response); if (!isset($response->access_token)) { return false; } return $response->access_token;
Additional Changes
Additionally, similar changes need to be made to the setExtendedAccessToken() function to ensure proper token extension and avoid potential issues with short-lived tokens. The updated code is as follows:
public function setExtendedAccessToken() { 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'), $params = 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; } //Version 2.3 and up. $response = json_decode($access_token_response); if (!isset($response->access_token)) { return false; } $this->destroySession(); $this->setPersistentData( 'access_token', $response->access_token ); }
By implementing these changes, developers can ensure their apps continue to function as expected under Facebook Graph API v2.3.
The above is the detailed content of Why are Facebook Graph API v2.3 Upgrades Causing Compatibility Issues?. For more information, please follow other related articles on the PHP Chinese website!