我正在创建一个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() );
}
我尝试使用谷歌搜索并搜索问题,但我找不到任何问题。对不起,我无法提供我在研究中得到的任何信息。
答案 0 :(得分:1)
此解决方案适用于我:
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 {}区域之外,我无法捕捉到这个例子。这是非常奇怪的,因为这行可以正常运行并且不会抛出任何异常。
答案 1 :(得分:-1)
我有同样的问题和解决方案是我忘了命名空间(我在另一个操作中),所以catch块的正确代码是:
catch(\Exception $e) {}