php - 无法捕获 `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
和初始反斜杠的多种变体,但我仍然无法捕获该特定异常。有什么可能导致这种情况吗?
解决方案
推荐阅读
- javascript - 为什么 app.use(fn, '/api/auth', require('./routes/auth.routes')) 错误?
- java - 如何以编程方式设置 RelativeLayout 的 layout_constraintTop_toBottomOf “父级”?(安卓)
- javascript - SyntaxError: Unexpected token < in JSON at position 0 如何解决
- c++ - 为什么 C++ 类构造函数参数不允许具有与类属性相同的标识符?
- python - 在 Jupyter 笔记本环境中使用“地图”对象
- python-3.x - 使用条件 Python 在列表中拆分字符串
- node.js - 如何在@get 端点中执行分页
- c# - c# 中的引用标识是什么?
- javascript - 测试 JS 时为事件模拟对象
- php - 如何在 php 中修复数组中的 Foreach 数组以返回结果