0

I'm trying to convert a curl request to Guzzle. In curl I'm getting the response where as in Guzzle it is giving error.

My curl request is

        ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode($client_id . ":" . $client_secret), 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded'));
        $url = '<toke endpoint>';
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            "grant_type=authorization_code&code=".$code."&redirect_uri=https://redirect-uri&scope=openid");

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        print_r($result);
        die;

For guzzle I've converted it as

 $response = $client->post('<token end point>',[
            'headers' => [
                'Authorization' => "Basic " . base64_encode($client_id . ":" . $client_secret),
                'Accept' => 'application/json',
                'Content-Type' => 'application/x-www-form-urlencoded',
            ], [
            'form_params' => [
                'grant_type' => 'authorization_code',
                'code' => $code,
                'redirect_uri' => '<redirect-uri>',
                'scope' => 'openid',
            ]]
        ]);

        print_r($response);
        echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
        die;

The error I'm getting is {"error_description":"CWOAU0025E: The grant_type parameter was invalid: unknown","error":"unsupported_grant_type"}

I used to get this in curl also, then I've passed it as url in CURLOPT_POSTFIELDS. In guzzle it seems not to work.

Fazeela Abu Zohra
  • 641
  • 1
  • 12
  • 31
  • Check if you have .htaccess handling for appending or removing trailing slashes in the URL. Because when you get a 301 from the initial POST request, the redirect becomes a GET and the parameters are lost. – Kyle Domingo May 24 '17 at 07:09
  • @KyleDomingo Yes I do have .htacces to remove index.php from url. In that case do we do? – Fazeela Abu Zohra May 24 '17 at 08:34
  • It could be your guzzle version, i think that in older versions you had to add your post fields trough method addPostFields() or setPostFields() – Denis Solakovic May 24 '17 at 09:12
  • @DenisSolakovic I'm using 6. "guzzlehttp/guzzle": "~6.0" – Fazeela Abu Zohra May 24 '17 at 09:31
  • @FazeelaAbuZohra , maybe you could find this post helpful: https://stackoverflow.com/questions/31456066/how-to-use-oauth-with-guzzle-5-or-better-with-guzzle-6 – Denis Solakovic May 24 '17 at 09:39

0 Answers0