Skip to content

Instantly share code, notes, and snippets.

@jaredhabeck
Created January 17, 2012 20:32
Show Gist options
  • Save jaredhabeck/1628718 to your computer and use it in GitHub Desktop.
Save jaredhabeck/1628718 to your computer and use it in GitHub Desktop.
Facebook PHP SDK - check access token validity
<?php
// this follows the assumption that you've already instantiated the facebook SDK
// e.g. $facebook = new Facebook();
// The key is the first api() call - you need to try to access something which requires the access token
// you are attempting to validate.
// e.g. if your access token has *no* special permissions you could use $facebook->api('/me'),
// which is the most basic permission level.
try {
$facebook->api('/me');
} catch (Exception $e) {
if ($e->getType() == "OAuthException") {
$url = $this->facebook->getLoginUrl(array(
'redirect_uri' => "<your redirect uri which has to be set as owned by the app>"
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment