首页 > 解决方案 > 无法捕获 `FacebookAuthenticationException`,但其他异常都可以

问题描述

我正在尝试访问 facebook API。当我使用过期的页面访问令牌时,API 会抛出\Facebook\Exceptions\FacebookAuthenticationException,我应该获取一个新的。但是,我无法捕捉到这个异常,只有更通用的FacebookSDKException

<?php
namespace my\company;

require_once __DIR__ . '/vendor/autoload.php';

$fb = new \Facebook\Facebook([
    'app_id' => '<app_id>',
    'app_secret' => '<app_secret>',
    'default_graph_version' => 'v5.0',
]);

$access_token = '<access token>';
$query = '/mypage/feed?message,id';
try {
    $response = $fb->get($query, $access_token);
}
catch (\Facebook\Exceptions\FacebookAuthenticationException $e) {
    // handle it
}

这失败了:

Fatal error:  Uncaught Facebook\Exceptions\FacebookAuthenticationException: Error validating access token: Session has expired on Sunday, 19-Jan-20 09:00:00 PST. The current time is Saturday, 25-Jan-20 06:07:17 PST. in C:\myproject\wp-content\plugins\someplugin\vendor\facebook\graph-sdk\src\Facebook\Exceptions\FacebookResponseException.php:89 Stack trace:
#0 C:\myproject\wp-content\plugins\someplugin\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(210): Facebook\Exceptions\FacebookResponseException::create(Object(Facebook\FacebookResponse))
#1 C:\myproject\wp-content\plugins\someplugin\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(255): Facebook\FacebookResponse->makeException()
#2 C:\myproject\wp-content\plugins\someplugin\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(82): Facebook\FacebookResponse->decodeBody()
#3 C:\myproject\wp-content\plugins\someplugin\vendor\facebook\graph-sdk\src\Facebook\FacebookClient.php(224) in C:\myproject\wp-content\plugins\someplugin\vendor\facebook\graph-sdk\src\Facebook\Exceptions\FacebookResponseException.php on line 89

但是,我可以赶上\Facebook\Exceptions\FacebookSDKException\Exception罚款,而不是在这个非常相似的问题!那里推荐的解决方案也不起作用。我尝试添加use Facebook,use \Facebook和初始反斜杠的多种变体,但我仍然无法捕获该特定异常。有什么可能导致这种情况吗?

标签: phpfacebook

解决方案


推荐阅读