我想使用 Symfony HttpClient 请求从 OAUTH 服务器检索用户信息,但遇到错误响应时我无法直接获取响应,因为客户端抛出异常。
我的用户提供者:
public function loadUserByUsername($username)
{
try {
$response = $this->httpClient->request(
'GET',
$this->baseUrl . '/userinfo',
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'auth_bearer' => $username
]
);
var_dump($response->toArray());die;
} catch (\Exception $e) {
var_dump($e->getMessage());die;
throw new UsernameNotFoundException($e->getMessage());
}
}
当我调用$response->toArray()
or$response->getContent()
时,会引发异常,并且在 Postman 上收到以下错误消息
<pre class='xdebug-var-dump' dir='ltr'>
<small>/var/www/html/backend/src/Security/Provider/KeycloakUserProvider.php:50:</small><small>string</small> <font color='#cc0000'>'HTTP/2 401 returned for "https://oauth_server/userinfo".'</font> <i>(length=127)</i>
</pre>
浏览器收到的响应示例:
{"error":"invalid_request","error_description":"Token not provided"}
为什么我不能通过调用直接访问响应$response->toArray()
?