When I post params , Postman not get response, loading forever - laravel

Here my Controller
$http = new \GuzzleHttp\Client; $response = $http->post('http://127.0.0.1:8000/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => 'vZ2wi8dOIJLueXRzP4Dqvraad0V1GBj3DDy8RaXj,
'username' => $request->username,
'password' => $request->password,
]
]);
return $response->getBody();
When I post params like below
Not get the response.
Please help me

Related

How to correctly authenticate users with same email and password?

In my users table i have additional column that stores the information to which specific scope user belongs and for our application needs a different scope can have same email and password.
My problem is that when i call /oauth/token URL i can get a token for a wrong user if there are 2 rows with same email and password.
Is there an way to call that method and passing additional custom field?
For example, if i have:
$response = $http->request('POST', env('APP_URL') . '/api/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $oClient->id,
'client_secret' => $oClient->secret,
'username' => $request->email,
'password' => $request->password,
'scope' => 'business',
],
]);
is there any way to add additional parameter in there like
$response = $http->request('POST', env('APP_URL') . '/api/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $oClient->id,
'client_secret' => $oClient->secret,
'username' => $request->email,
'password' => $request->password,
'user_role' => 'example_role'
'scope' => 'business',
],
]);

Guzzle authentication returning 400 error

I'm trying to authenticate in an api sandbox but error 400 is returning me, I already got it through an extension but in the code with guzzle it's not working, I need to pass the Header parameter with encrypted authorization as it is.
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Basic $token']]);
$response = $client->post('', [
'grant_type' => 'password',
'scope' => 'forintegration',
'username' => '',
'password' => '',
]);
$body = $response->getBody();
print_r(json_decode((string) $body));
Working with:
$client = new \GuzzleHttp\Client();
$url = "";
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic $token'
],
'form_params' => [
'grant_type' => 'password',
'scope' => 'forintegration',
'username' => '',
'password' => ''
]
]);
$body = $response->getBody();
print_r(json_decode((string) $body));

Guzzle Not Sending Grant Type to Laravel Passport

Im a little stumped with my code, I am running Laravel 6 with Guzzle Http Client version 6.3.3.
I have opted to use a trait which I use on my API Gateway for communicating with micro services instead of bloating code base with repeated code.
The Trait
public function performRequest($method, $requestUrl, $formParams = [], $headers =[])
{
$core = env('CORE_URI');
$client = new Client([
'base_uri' => $core,
]);
$response = $client->request($method, $requestUrl, ['form_params' => $formParams, 'headers' => $headers]);
return $response->getBody()->getContents();
}
The failing code (Not sending the OAuth Grant Type Password even though it works using postman)
$core_client_id = env('CORE_CLIENT_ID');
$core_client_secret = env('CORE_CLIENT_SECRET');
$username = $request->input('username');
$password = $request->input('password');
return $this->performRequest('POST','/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $core_client_id,
'client_secret' => $core_client_secret,
'username' => $username,
'password' => $password,
'scope' => '',
],
'headers' => [
'content-type' => 'multipart/form-data',
]
]);
The Exception Guzzle is returning is 400 Bad Request 'Unsupported Grant Type'
I fixed it by removing the headers and form params and changing my code to send the data as an array instead.
Working Code
public function attemptLogin(Request $request)
{
$core_client_id = env('CORE_CLIENT_ID');
$core_client_secret = env('CORE_CLIENT_SECRET');
$username = $request->input('username');
$password = $request->input('password');
$data = array(
'grant_type' => 'password',
'client_id' => $core_client_id,
'client_secret' => $core_client_secret,
'username' => $username,
'password' => $password,
'scope' => '',
);
return $this->performRequest('POST','/oauth/token', $data);
}
I searched on the internet a bit and found out that OAuth2 specification for header Content-Type is "application/x-www-form-urlencoded" . To fix your problem simply remove 'content-type' => 'multipart/form-data' from 'headers'
Here is a complete code
$core_client_id = env('CORE_CLIENT_ID');
$core_client_secret = env('CORE_CLIENT_SECRET');
$username = $request->input('username');
$password = $request->input('password');
return $this->performRequest('POST','/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $core_client_id,
'client_secret' => $core_client_secret,
'username' => $username,
'password' => $password,
'scope' => '',
],
]);

Can't post data via Guzzle request to Laravel 5.4

I'm having troubles posting data to my production Laravel controller to store a post request made with Guzzle from my local WAMP server. I can successfully return data with a get request, but posting data does not seem to work. I have Oauth2 setup using Laravel Passport.
The following is my Guzzle post request from the WAMP server.
$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.website.com/oauth/token', [
'form_params' => [
'client_id' => 99,
'client_secret' => '***',
'grant_type' => 'password',
'username' => 'username#mail.com',
'password' => 'password',
'scope' => '*',
],
]);
$auth = json_decode((string) $response->getBody()->getContents());
$data = [
'first_name' => 'Post', 'last_name' => 'Man',
'email' => 'postman#mail.com', 'phone' => '0400000000',
'country' => 'Internet', 'state' => 'HTTP'
];
$json_data = json_encode($data);
$header = array('Authorization' => 'Bearer '.$auth->access_token, 'Content-Type' => 'application/json');
$response = $client->post('https://www.website.com/api/store_data',
['body' => $json_data, 'headers' => $header]);
$stream = $response->getBody()->getContents();
dd($stream);
Returns:
"{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}"
And when I try to store the data in my production controller nothing comes across in the request:
$enquiry = new Enquiry;
$enquiry->first_name = $request->get('first_name');
$enquiry->last_name = $request->get('last_name');
....
$enquiry->save();
Post data without json_encode & use form_params instead of body
$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.website.com/oauth/token', [
'form_params' => [
'client_id' => 99,
'client_secret' => '***',
'grant_type' => 'password',
'username' => 'username#mail.com',
'password' => 'password',
'scope' => '*',
],
]);
$auth = json_decode((string) $response->getBody()->getContents());
$data = [
'first_name' => 'Post', 'last_name' => 'Man',
'email' => 'postman#mail.com', 'phone' => '0400000000',
'country' => 'Internet', 'state' => 'HTTP'
];
$header = array('Authorization' => 'Bearer '.$auth->access_token, 'Content-Type' => 'application/json');
$response = $client->post('https://www.website.com/api/store_data',
['form_params' => $data, 'headers' => $header]);
$stream = $response->getBody()->getContents();
dd($stream);
For store the data:
$post = $request->all();
Enquiry::create([
'first_name' => $post['first_name'],
'last_name' => $post['last_name']
]);

Laravel Passport Request Token Don't response

I request passport token using Requesting Tokens but it is loading in postman nothing response.
This is my source code
$http = new Client;
$response = $http->post('http://localhost:8000/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => '3',
'client_secret' => 'AMZucpstZeT1RvZwBl96FNIqzDToOy32yFQglzfy',
'username' => 'dtowne#example.org',
'password' => 'secret',
'scope' => '',
],
]);
if I change http://localhost:8000 to your-app.com data response null

Resources