0

I am trying to consume a service that I exposed from a project that I have in c #, the problem is that when consuming the service from php I get the following error. {"error":"unsupported_grant_type"}

Code:

function __construct() {

    # data needs to be POSTed to the Play url as JSON.
    # (some code from http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl)
    $data = array("grant_type" => "password", "username" => "Administrador", "password" => "xxxx");
    $data_string = json_encode($data);

    print_r($data_string);

    $ch = curl_init('https://integraciones.intergrupo.com/rest/oauth/token');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded')
    );
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);

    echo $result;        // Outputs the JSON decoded data
}
  • hm... you send a request to `../oauth/token` but have grant_type "password". Could it be the wrong endpoint for pwd authentication? – Jeff Dec 20 '18 at 22:11
  • I doubt you need to (or should) json_encode `$data` when sending as form-urlencoded. See here: https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code - in the linked example of lornajane they use `application/json`. – Jeff Dec 20 '18 at 22:16
  • Thank you very much, that was the solution! – Sergio Londoño Osorio Dec 20 '18 at 22:20
  • I'll make it an answer then, feel free to accept it! – Jeff Dec 20 '18 at 22:27

1 Answers1

0

You are sending a header 'Content-Type: application/x-www-form-urlencoded'.
With that you shouldn't json_encode $data, but just add the array to POSTFIELDS:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

OR change the header to 'Content-Type: application/json'

Jeff
  • 6,807
  • 1
  • 13
  • 31