我想使用Laravel Passport生成刷新和访问令牌 我遵循了这篇文章:Https://dev.to/mahmudulhsn/laravel-passport-api-authentication-with-access-anccess-and-refresh-token-16d0 但是当我去生成刷新并访问令牌时 它给了我这个错误:
{
"message": "cURL error 28: Operation timed out after 30003 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://localhost:8000/oauth/token",
"exception": "Illuminate\\Http\\Client\\ConnectionException",
"file": "C:\\xampp\\htdocs\\passport-authentication\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Client\\PendingRequest.php",
"line": 941,
"trace": [
{
"file": "C:\\xampp\\htdocs\\passport-authentication\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php",
"line": 338,
"function": "Illuminate\\Http\\Client\\{closure}",
"class": "Illuminate\\Http\\Client\\PendingRequest",
"type": "->"
},
{
"file": "C:\\xampp\\htdocs\\passport-authentication\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Client\\PendingRequest.php",
"line": 905,
"function": "retry"
},
{
这是我生成访问和刷新令牌的代码:
public function register(RegisterRequest $request): JsonResponse
{
$userData = $request->validated();
$userData['email_verified_at'] = now();
$user = User::create($userData);
$response = Http::post(env('APP_URL') . '/oauth/token', [
'grant_type' => 'password',
'client_id' => env('PASSPORT_PASSWORD_CLIENT_ID'),
'client_secret' => env('PASSPORT_PASSWORD_SECRET'),
'username' => $userData['email'],
'password' => $userData['password'],
'scope' => '',
]);
$user['token'] = $response->json();
return response()->json([
'success' => true,
'statusCode' => 201,
'message' => 'User has been registered successfully.',
'data' => $user,
], 201);
}
注:我所有的崇高和配置都是正确的,但仍然给我这个错误
我期望它可以正常工作,但它给了我错误
您是通过XAMPP运行这个项目吗?如果不是,是通过码头或任何容器吗?
如果您正在通过Docker容器运行Laravel项目,则该端口通常与主机机器和容器不同。
,但是,如果您确实正在通过XAMPP运行并使用Laravel的运行,那么您可能需要运行2个Artisan Server命令。示例)
1st终端:运行
php artisan serve --port=8070
2ND终端:运行php artisan serve --port=8071
然后在您的代码中,更改以下内容:
$response = Http::post('http://localhost:8001/oauth/token', [
'grant_type' => 'password',
'client_id' => env('PASSPORT_PASSWORD_CLIENT_ID'),
'client_secret' => env('PASSPORT_PASSWORD_SECRET'),
'username' => $userData['email'],
'password' => $userData['password'],
'scope' => '',
]);
示例响应:
{
"success":true,
"statusCode":201,
"message":"User has been registered successfully.",
"data":{
"name":"Test",
"email":"[email protected]",
"updated_at":"2025-02-06T06:29:53.000000Z",
"created_at":"2025-02-06T06:29:53.000000Z",
"id":4,
"token":{
"token_type":"Bearer",
"expires_in":1296000,
"access_token":"some token",
"refresh_token":"some refresh token"
}
}
}
希望这回答您的问题。 thanks.