【发布时间】:2021-07-05 03:45:43
【问题描述】:
我正在设置 Laravel Passport 的客户端授权并使用 Guzzle 检索令牌,但我收到 401 Unauthorized 错误。
我使用:php artisan passport:client --client 创建了客户端。
生成的客户端是:
Client ID: 1
Client secret: NmJsEVFClXVqWJuQnZTWA3bnZEVZ0KaC13anHZt1
我将这些保存在我的 .env 文件中并运行了命令
php artisan config:clear
这是我的代码:
$url = 'http://hub.local/oauth/token';
$client_secret = env('CLIENT_SECRET');
$client_id = env('CLIENT_ID');
$client = new Client();
try {
$response = $client->post($url, [
'json' => [
'grant_type' => 'client_credentials',
'client_id' => $client_id,
'client_secret' => $client_secret,
],
'headers' => [
'Content-Type' => 'application/json',
],
]);
return $response;
} catch (RequestException $e) {
dd($e);
} catch (Exception $e) {
dd($e);
}
以及由此产生的错误信息:
ClientException {#660 ▼
-request: Request {#649 ▶}
-response: Response {#657 ▶}
-handlerContext: []
#message: """
Client error: `POST http://hub.local/oauth/token` resulted in a `401 Unauthorized` response:
{"error":"invalid_client","error_description":"Client authentication failed","message":"Client authentication failed"}
"""
#code: 401
#file: "C:\repos\client\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php"
#line: 113
当我在 Insomnia 中使用相同的参数和值尝试请求时,请求有效。 好像还不能发图。 https://i.imgur.com/w6Ollin.jpg https://imgur.com/fXpzKjj.jpg
我正在使用 Laravel 5.8.12、Guzzle 6.3.3 和 Passport 7.2.2。 我错过了什么?
【问题讨论】:
-
请不要在此处发布密码或 API 令牌(客户端机密)!
-
@PowerStat 感谢您的提醒。我知道发布身份验证凭据的危险,但这是一个用于测试的本地实例,我想避免担心我是否在请求中使用了正确的客户端密码。在阅读与该主题相关的其他问题时,在某些情况下这是一个错误,所以这只是我提前表明这对我来说不是一个可能的错误来源。干杯。
标签: laravel oauth-2.0 laravel-passport