0

I have been struggling with the following code for some time. I get the following error: {"error":"invalid_request","error_description":"invalid grant type"}.

Some more documentation on the API that I am working on is available here::

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://id.shoeboxed.com/oauth/token",
   CURLOPT_RETURNTRANSFER => true,
  CURLOPT_SSL_VERIFYPEER => false, 
  CURLOPT_POST => 1,  
  CURLOPT_POSTFIELDS => "{\"code\": \"['code']\",\"grant_type\":\"authorization_code\",\"redirect_uri\": \"http://website.com/foursquare2.php\",\"client_id\": \"f8de67be8dc84e449203fcdd4XXXXXXX\",\"client_secret\": \"HS5ZeIVsKW0/qqiO9/XcdeWqnF8vtzQrpY8gcdrxg0BXNZXXXXXXX\"}",
  CURLOPT_HTTPHEADER => array(
    "application/x-www-form-urlencoded" 
  )
));


/*
//Another Attempt at it is below
curl -d code=['code'] \
    -d grant_type=authorization_code \
    --data-urlencode redirect_uri='http://website.com/foursquare2.php' \
    -u f8de67be8dc84e449203fcdd44abad5a:HS5ZXXXXXXX/qqiO9/XcdeWqnF8vtzQrpY8gcdrxg0BXNXXXXXXX \
    -XPOST https://id.shoeboxed.com/oauth/token 
*/


$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>
Rick
  • 35
  • 1
  • 4
  • You set `application/x-www-form-urlencoded` (which is correct) but your body is encoded in JSON. Just change your body and it will work fine – Spomky-Labs Apr 21 '18 at 09:34
  • What exactly should I encode? Just the redirect_uri ? I tried that and get the same error. I tried to encode all of the http web addresses and get a http not found address for the posting address. I would be willing to pay for your help to do this (preferably) or just guide me again. – Rick Apr 22 '18 at 21:13
  • Nope, the whole body: `CURLOPT_POSTFIELDS => http_build_query(['code' => '[code]', 'grant_type' => 'authorization_code', 'redirect_uri' => 'http://website.com/foursquare2.php', 'client_id' => 'f8de67be8dc84e449203fcdd4XXXXXXX', 'client_secret' => 'HS5ZeIVsKW0/qqiO9/XcdeWqnF8vtzQrpY8gcdrxg0BXNZXXXXXXX'])` – Spomky-Labs Apr 25 '18 at 09:09

1 Answers1

1

From https://secure.php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-notes

Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

In the case of the API you are using, you must use application/x-www-form-urlencoded

<?php

$curl = curl_init();

$postFields = array(
  'code'          => 'code',
  'grant_type'    => 'authorization_code',
  'redirect_uri'  => 'http://website.com/foursquare2.php',
  'client_id'     => 'f8de67be8dc84e449203fcdd4XXXXXXX',
  'client_secret' => 'HS5ZeIVsKW0/qqiO9/XcdeWqnF8vtzQrpY8gcdrxg0BXNZXXXXXXX',
  );

curl_setopt_array(
    $curl, array(
        CURLOPT_URL => "https://id.shoeboxed.com/oauth/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false, 
        CURLOPT_POST => 1,  
        CURLOPT_POSTFIELDS => http_build_query($postFields),
    )
);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}
Glen
  • 106
  • 8