algorithmicCoder
algorithmicCoder

Reputation: 6789

How do I determine if there is an active facebook access token?

I am using the PHP Facebook 3.0 SDK for the graph API and want a simple way to find out if there is currently an active token so that I can handle errors gracefully.

getUserAccessToken() is a protected method and so I can't seem to access it with the facebook object. There has to be a simple way to do

if(active token){ do stuff} else{ don't}

Help anyone?

Upvotes: 0

Views: 109

Answers (3)

bkaid
bkaid

Reputation: 52073

The Facebook PHP SDK Example has all the code you need for this:

<?php
require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '',
  'secret' => '',
));

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
?>

Upvotes: 1

Ayman Safadi
Ayman Safadi

Reputation: 11552

Try this:

try {
    $User = $Facebook->api('/me');
} catch(Exception $e) {
    echo 'No active token.';
    echo $e->getMessage();
}

Upvotes: 0

Floyd Wilburn
Floyd Wilburn

Reputation: 1842

Maybe you can use this type of code:

$data=file_get_contents('https://graph.facebook.com/me?access_token=xxx');
foreach ($http_response_header as $rh) if (substr($rh, 0, 4)=='HTTP') list(,$status,)=explode(' ', $rh, 3);
if ($status==200 && $data)
    //Token is good, parse data
else
    //Token is no good

Upvotes: 0

Related Questions