'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'



Solution 1:[1]

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

Solution 2:[2]

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' => '',
        ],
    ]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Entrepreneur AJ
Solution 2 jureispro