2

I have used Api on to Single Sign In Opt with in Laravel https://sso/{custom_path}/token like this Api created using jwt. And on my end in web application passing access token and content type in header to Api call using http client guzzle. With content type application/x-www-form-urlencoded with parameters in form_params. But in response i am getting missing grant_type. As i am passing grant_type in form_parms array. Is there any other way to resolve this issue. Any valueable response will be considered.

Code:

$uri = $this->userTokenAuthencticateUrl();
        $token = session('token')->access_token;
        $params['header'] = [
            "Content-Type: application/x-www-form-urlencoded",
            "Authorization: Bearer $token"
            ];
        $params['form_params'] = array(
                'grant_type' => 'xxxxx',
                'response_include_resource_name' => 'xxx',
                'audience' => 'xxxx', 
                'permission' => 'xxxxxx',
            );
            $response = Http::post($uri, $params);
            dd($response->json());

Ressponse:

array:2 [▼
  "error" => "invalid_request"
  "error_description" => "Missing form parameter: grant_type"
]
Priya Negi
  • 117
  • 3
  • 15

1 Answers1

5

As you are using HTTP Client. You need to change your code. You do not need to pass Content-Type as application/x-www-form-urlencoded in your header and I believe the Authorization token is passed separately in headers you can pas it in your params.

$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;

 $params = array(
        'grant_type' => 'xxxxx',
        'response_include_resource_name' => 'xxx',
        'audience' => 'xxxx', 
        'permission' => 'xxxxxx',
 );
 $response = Http::asForm()->withHeaders([
        'Authorization' => 'Bearer ' . $token
     ])->post($uri, $params);

 dd($response->json());

Method 2:

It is also mentioned in docs

If you would like to quickly add an Authorization bearer token header to the request, you may use the withToken method so you can do like this as well

$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;

 $params = array(
        'grant_type' => 'xxxxx',
        'response_include_resource_name' => 'xxx',
        'audience' => 'xxxx', 
        'permission' => 'xxxxxx',
 );
 $response = Http::asForm()->withToken($token)->post($uri, $params);

 dd($response->json());

See the doc for more details


Method 3:

You can even directly use guzzle as well.
define("form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );
try{
  $client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . $token]]);
  
  $guzzleResponse = $client->post(
                $api_url, [
                'form_params' => [
                    'grant_type' => 'xxxxx',
                    'response_include_resource_name' => 'xxx',
                    'audience' => 'xxxx', 
                    'permission' => 'xxxxxx'
                ]
            ]);
    if ($guzzleResponse->getStatusCode() == 200) {
         $response = json_decode($guzzleResponse->getBody(),true);
         //perform your action with $response 
    } 
}
catch(\GuzzleHttp\Exception\RequestException $e){
   // you can catch here 400 response errors and 500 response errors
   // see this https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600
}catch(Exception $e){
   //other errors 
}
bhucho
  • 3,903
  • 3
  • 16
  • 34
  • always use http request with try catch as there is no guarantee that api request will alway s have 200 response, If you have added then it is ok – bhucho Oct 06 '20 at 15:56
  • I have used both your mentioned first and second method its giving same response with message as defined in question. For third method with try catch where you have passed header to add token with in exception handling? – Priya Negi Oct 06 '20 at 17:34
  • my bad forgot in method3, edited now but I wonder why are you getting such errors in HTTP class – bhucho Oct 06 '20 at 18:04
  • If 'headers' is not recognised by laravel, you need to add `define("headers", \GuzzleHttp\RequestOptions::HEADERS );` similar to one I have added for form_params constant – bhucho Oct 06 '20 at 18:05
  • you do not need to add Content-Type: application/x-www-form-urlencoded in header if you are using any of the methods written above – bhucho Oct 07 '20 at 04:28