无效的授权类型 - oauth 2.0-获取令牌

时间:2018-04-20 17:08:54

标签: php curl oauth oauth-2.0 oauth2client

我一直在努力使用以下代码。我收到以下错误:{“error”:“invalid_request”,“error_description”:“无效的授权类型”}。

有关我正在处理的API的更多文档可用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;
}

?>

1 个答案:

答案 0 :(得分:0)

来自This section of create-react-app's README

  

将数组传递给CURLOPT_POSTFIELDS会将数据编码为multipart / form-data,而传递URL编码的字符串会将数据编码为application / x-www-form-urlencoded。

如果您使用的是API,则必须使用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;
}