向Content-Type:application / x-www-form-urlencoded的oauth2密码授予类型的PHP CURL请求

问题描述 投票:1回答:1

正在尝试使用需要Content-Type的API:application / x-www-form-urlencoded来发行令牌。以下curl命令有效

curl -i \
  -X POST \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -u SwVl97HRUgWHutJGzO1wt1JMjI5JVV62:SgnyQAA8ap0pPcFf \
  -d 'grant_type=password&username=user&password=pass' \
  https://api.test.com/identity/v1-sandbox/token

但是在PHP中发布请求时,即>]

$params = array(
  "client_id" => "SwVl97HRUgWHutJGzO1wt1JMjI5JVV62",
  "client_secret" => "SgnyQAA8ap0pPcFf",
  "username" => "user",
  "password" => "pass",
  "grant_type" => "password");

$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, urlencode('user').':'.urlencode('pass'));

// Remove comment if you have a setup that causes ssl validation to fail
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$postData = "";

//This is needed to properly form post the credentials object
foreach($params as $k => $v)
{
   $postData .= $k . '='.urlencode($v).'&';
}

$postData = rtrim($postData, '&');

curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
echo "Performing Request...";

$json_response = curl_exec($curl);
//print_r($json_response);

print_r(curl_getinfo($curl));

API拒绝我的凭据。即

 {
                    "code":"401.01.001",
                    "error":"unauthorized",
                    "message":"Unauthorized - Please check your credentials."
 }

[当我检查curl请求标头时,我发现内容类型已更改为application / json,但我已请求它使用application / x-www-form-urlencoded,即。

[url] => https://api.test.com/identity/v1-sandbox/token
[content_type] => application/json
[http_code] => 401
[header_size] => 166
[request_size] => 314

有人遇到上述问题吗?

正在尝试使用需要Content-Type的API:application / x-www-form-urlencoded来发行令牌。以下curl命令可以使curl -i \ -X POST \ -H'Content-Type:application / x-www -...

types oauth-2.0 request passwords grant
1个回答
0
投票

要设置标题,您必须使用:CURLOPT_HTTPHEADER

© www.soinside.com 2019 - 2024. All rights reserved.