Skip to content

Instantly share code, notes, and snippets.

@sloped
Created March 5, 2012 20:43
Show Gist options
  • Save sloped/1980961 to your computer and use it in GitHub Desktop.
Save sloped/1980961 to your computer and use it in GitHub Desktop.
Simple Function to add to Facebook's facebook-php-sdk to grab an extended token for a user who has been authorized. Should be used when extending the BaseFacebook Class
<?php
function extendAccessToken() {
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;
}
$response_params = array();
parse_str($access_token_response, $response_params);
if (!isset($response_params['access_token'])) {
return false;
}
return $response_params['access_token'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment