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;
4
  • can I have Guzzle request code here? Commented Aug 7, 2018 at 10:27
  • Updated @VasimVanzara.. could you help? Commented Aug 7, 2018 at 10:49
  • check your log file is there any error? because above is perfect call. Commented Aug 7, 2018 at 10:52
  • This answer resolved my problem. Commented Oct 30, 2019 at 16:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I believe that the return should be (string) $request->getBody();.
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.