dongyoufo5672 2014-07-18 06:07
浏览 68

使用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
    $this->setUserConnected();

    // 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.

  • 写回答

2条回答 默认 最新

  • dpf71390 2015-04-17 06:55
    关注

    This solution works for me:

    try{
        $hybridauth = new Hybrid_Auth($config);
        $adapter = $hybridauth->authenticate($service);
    }catch(Exception $ex){
        var_dump($ex);
        return;
    }
    $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.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c