php - Cannot catch an Exception while authenticating user with HybridAuth


I'm creating a WordPress plugin that automatically publishes the post to Facebook when a new post is added to the site. For the same reason user needs to authenticate his Facebook account. The code I use for this purpose is:-

try {

        $fp_hybridauth  = new Hybrid_Auth( $hybrid_config );

        $fp_hybridauth->authenticate( "facebook" ); //this function does the job of authenticating user and it is causing the exception to be thrown 

        update_option( "session_data", $fp_hybridauth->getSessionData() );

        wp_redirect( site_url("/wp-admin/admin.php?page=facebook-publish&tab=api&fbauth=success") );

    catch( Exception $e ){

        echo $e->getMessage();


This code works perfectly if user permits the permission in Facebook oAuth dialog, but if user denies to give permission, it throws an exception that I can't seem to catch:-

Fatal error: Uncaught exception 'Exception' with message 'Authentication failed! The user denied your request.' in /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybridauth/Hybrid/Providers/Facebook.php:86 Stack trace: #0 /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybridauth/Hybrid/Endpoint.php(182): Hybrid_Providers_Facebook->loginFinish() #1 /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybridauth/Hybrid/Endpoint.php(58): Hybrid_Endpoint::processAuthDone() #2 /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybridauth/index.php(15): Hybrid_Endpoint::process() #3 {main} Next exception 'Exception' with message 'Authentication failed! The user denied your request.' in /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybridauth/Hybrid/Auth.php:147 Stack trace: #0 /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybrid in /home/pramodjodhani/public_html/dev/wp-content/plugins/facebook-publish/lib/class/hybridauth/Hybrid/Auth.php on line 147

The code that is written in the Hybrid/Providers/Facebook.php file is:-

function loginFinish()
    // in case we get error_reason=user_denied&error=access_denied
    if ( isset( $_REQUEST['error'] ) && $_REQUEST['error'] == "access_denied" ){ 
        throw new Exception( "Authentication failed! The user denied your request.", 5 ); //THIS IS LINE NUMBER 86 

    // try to get the UID of the connected user from fb, should be > 0 
    if ( ! $this->api->getUser() ){
        throw new Exception( "Authentication failed! {$this->providerId} returned an invalid user id.", 5 );

    // set user as logged in

    // store facebook access token 
    $this->token( "access_token", $this->api->getAccessToken() );

I tried Googling and searching for the issue but I couldn't find anything wrong with this. Sorry I can't provide anything that I got in my research.




This solution works for me:

    $hybridauth = new Hybrid_Auth($config);
    $adapter = $hybridauth->authenticate($service);
}catch(Exception $ex){
$user_profile = $adapter->getUserProfile();

Exception fires Ok. But if I move line $hybridauth = new Hybrid_Auth($config);

outside the try{} area then I can't catch this exeption. This is quite wierd because this line works Ok and doesn't throw any exeption.




I had the same problem and solution was that i forgot about namespace (i was operating inside another one), so the correct code for catch block is:

catch(\Exception $e) {}

People are also looking for solutions to the problem: php - Add dynamic class in content box in OpenCart


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.