3

Im using Laravel passport for API authentication. I have two routes /api/login and /oauth/token

Since I cannot hardcode my client id and the login receives from JS and the params and client id is hardcoded inside a login method(laravel), Im trying to post the values using Guzzle (6.0) to oauth/token (POST requests).

I followed a youtube video and there it works but not mine. Iam using 5.6, not sure which version was in the video. Could someone help?

Below is the Video

https://www.youtube.com/watch?v=HGh0cKEVXPI&t=838s

Below is the code

$http = new GuzzleHttp\Client();
$request = $http->post(URI, '/oauth/token', [
    'form_params' => [
        'username' => 'bar',
        'password' => 'xxxxxx',
        'client_id' => 2,
        'grant_type' => 'password',
        'client_secret' => '00000000000000000'
    ]
]);

return $request;
The Keeper
  • 341
  • 3
  • 14

3 Answers3

1

You are not getting the response only returning guzzle $request initalization so add getBody()

   $http = new GuzzleHttp\Client();
    $request = $http->post(URI, '/oauth/token', [
        'form_params' => [
            'username' => 'bar',
            'password' => 'xxxxxx',
            'client_id' => 2,
            'grant_type' => 'password',
            'client_secret' => '00000000000000000'
        ]
    ]);

    return $request->getBody();
Leo
  • 6,322
  • 5
  • 18
  • 42
0

You should check the status first to make sure that everything is okay by using

$request->getStatusCode(); 

You can get your response by

$request->getBody();

you can see also full documentation of using GuzzulHttp from Here http://docs.guzzlephp.org/en/stable/

Dev_meno
  • 126
  • 12
0

I think you are trying to request in build-in server. So You try two servers to send the request. It will be working.

Like localhost:8000 and localhost:9000 server

Use this command

php artisan serve

php artisan serve --port=9000

Thanks.

Manoj kumar
  • 119
  • 1
  • 4