将cURL请求命令转换为Coldfusion(cfhttp)

时间:2016-08-30 22:02:14

标签: curl coldfusion cfhttp

我正在尝试使用ColdFusion CFHTTP来模仿以下cURL请求

curl -u myCl13nt1D:my53cR3tK3Y -X POST --data "grant_type=password&username=x&password=y" https://www.sitename.net/token
<cfhttp url="https://www.sitename.net/token" method="post" username="x" password="y">
  <cfhttpparam type="url" name="client_id"     value="myCl13nt1D" />
  <cfhttpparam type="url" name="client_secret" value="my53cR3tK3Y" />

  <!--- attempt 1 - without using cfhttp username/password attributes
  <cfhttpparam type="formfield" name="grant_type" value="password" />
  <cfhttpparam type="formfield" name="username" value="x" />
  <cfhttpparam type="formfield" name="password" value="y" /> 
  --->
  <!--- attempt 2 - without using cfhttp username/password attributes
  <cfhttpparam type="formField" name="data" value="grant_type=password&username=x&password=y" />
  --->

  <!--- attempt 3 - using cfhttp username/password attributes --->
  <cfhttpparam type="formField" name="data" value="grant_type=password" />
</cfhttp>

在命令提示符下,cURL请求可以恢复预期结果但使用CFHTTP我收到以下错误(状态码401未经授权)

{"error":"unauthorized_client","error_description":"That application is not registred or blocked"}

我尝试了不同的方法来传递所需的参数,但它们都返回相同的错误。

1 个答案:

答案 0 :(得分:3)

-u myCl13nt1D:my53cR3tK3YBasicAuth,并在username中分为password / cfhttp个属性。试试这个:

<cfhttp url="https://www.sitename.net/token" method="post" username="myCl13nt1D" password="my53cR3tK3Y">
  <cfhttpparam type="formfield" name="grant_type" value="password" />
  <cfhttpparam type="formfield" name="username"   value="x"        />
  <cfhttpparam type="formfield" name="password"   value="y"        />
</cfhttp>

查看此请求,您使用BasicAuth进行身份验证,并使用端点的用户名/密码登录机制进行授权,很可能是OAuth2