Search code examples
phpapifacebook-graph-apierror-handlingfacebook-oauth

Uncaught OAuthException - how to catch this error?


I have the below error. How am I supposed to catch this error?

Fatal error: Uncaught OAuthException: Error validating access token: User 638720122 has not authorized application 207445576002891. thrown in /var/www/clients/client1/web12/web/socialmediaping/fblibrary/base_facebook.php on line 1039

I have the below snippet of code where I beleive I do attempt to manage the error.

// Attempt to query the graph:
$graph_url = "https://graph.facebook.com/me?"
  . "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);

//Check for errors 
if ($decoded_response->error) {
    $facebookAuth = FALSE;
}  

Further down I redirect the user to facebook to authoise if $facebookAuth == FALSE but this isnt working, so What should I be doing?

Many thanks in advance for your help.


Solution

  • Dont screw your base_facebook.php! Simply use a try/catch block around your graph calls:

    try {
    
        // check if facebook can get user
        $facebookUser = $facebook->getUser();
        $facebookUser = $facebook->api('me?fields=id,first_name,last_name');
    
    } catch (Exception $e) {
        // user is not logged in
    }