phplaravelpassport.js

Laravel passport: Error cURL error 7: Failed to connect to localhost port 8080 after 0 ms


public function getTokenAndRefreshToken(OClient $oClient) {
    $http = new Client;
    

    $response = $http->request('POST', 'http://localhost:8080/oauth/token', [
            'grant_type' => 'password',
            'client_id' => 20,
            'client_secret' => 'R9pywYcu9cRdRBJWv*********rJBwdSOSq',
            'username' => 'kh123@mail.com',
            'password' => "123456",
            'scope' => '*',
    ]);
    $result = json_decode((string) $response->getBody(), true);
    return response()->json($result, $this->successStatus);
}

Illuminate\Http\Client\ConnectionException: cURL error 7: Failed to connect to localhost port 8080 after 0 ms: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://localhost:8080/oauth/token in file /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php on line 856


Solution

  • When you call from postman or any external tool, you access from outside and you need to specify the port.

    When running through the server, you don't need to specify the port.

    Recommendation: avoid usign a hardcoded URL and generate an internal dns to resolve oAuth server ip. /etc/hosts or similar