php - laravel passport cURL error 28: Operation timed out after 30001 milliseconds with 0 bytes received error - Stack Overflow

admin2025-04-15  1

i want to use laravel passport to generate refresh and access tokens i followed this Article : but when i go to generate refresh and access tokens it gives me this error :

{
    "message": "cURL error 28: Operation timed out after 30003 milliseconds with 0 bytes received (see .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"
        },
        {

and that is my code to generate access and refresh tokens:

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);
    }

Note : all my credientials and configuration are correct but still gives me this error

i was expecting it to work but instead it gives me error

i want to use laravel passport to generate refresh and access tokens i followed this Article : https://dev.to/mahmudulhsn/laravel-passport-api-authentication-with-access-and-refresh-token-16d0 but when i go to generate refresh and access tokens it gives me this error :

{
    "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"
        },
        {

and that is my code to generate access and refresh tokens:

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);
    }

Note : all my credientials and configuration are correct but still gives me this error

i was expecting it to work but instead it gives me error

Share Improve this question asked Feb 4 at 12:09 user24914143user24914143 114 bronze badges 1
  • That error shows 3 line numbers in various files. Have you looked at that code, have you shown that code here? I think maybe not – RiggsFolly Commented Feb 4 at 17:57
Add a comment  | 

1 Answer 1

Reset to default 1

Are you running this project through xampp? If not, is it through docker or any containers?

If you are running the laravel project through docker container, the port is usually going to be different from host machine and the containers.

However, if you are indeed running through xampp and using laravel's built-in dev server, then you might need to run 2 artisan server command.

Example)

1st terminal: Run php artisan serve --port=8070

2nd terminal: Run php artisan serve --port=8071

And then in your code, change the following:

$response = Http::post('http://localhost:8071/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' => '',
]);

You should see something like this in your terminal output.

Example request:

POST http://127.0.0.1:8070/api/register

{
    "name": "Test",
    "email": "[email protected]",
    "password": "password1234"
}

Example response:

{
   "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"
      }
   }
}

The reason for this is that the built-in laravel web server is a single thread process. I forgot where I saw about it, but since we are calling passport internally, it cannot handle the passport request. Thus, running a 2nd artisan serve command to handle the passport request will make it work. However, it is recommended to use a proper web server like Apache or Nginx. They can handle it without any problem.

Hope this answers your question.

Thanks.

转载请注明原文地址:http://www.anycun.com/QandA/1744721416a86702.html