gpt4 book ai didi

php - 使用 HybridAuth 验证用户时无法捕获异常

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:54 24 4
gpt4 key购买 nike

我正在创建一个 WordPress 插件,当有新帖子添加到网站时,它会自动将帖子发布到 Facebook。出于同样的原因,用户需要验证他的 Facebook 帐户。我为此目的使用的代码是:-

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();

}

如果用户在 Facebook oAuth 对话框中允许权限,此代码将完美运行,但如果用户拒绝授予权限,它会抛出一个我似乎无法捕获的异常:-

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

在 Hybrid/Providers/Facebook.php 文件中编写的代码是:-

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() );
}

我尝试使用谷歌搜索并搜索问题,但我找不到任何问题。抱歉,我无法提供我在研究中得到的任何信息。

最佳答案

这个解决方案适合我:

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

异常触发 好的。但是如果我移动线 $hybridauth = new Hybrid_Auth($config);

在 try{} 区域之外,我就无法捕捉到这个异常。这很奇怪,因为这条线工作正常并且不会抛出任何异常。

关于php - 使用 HybridAuth 验证用户时无法捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24818243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com