【问题标题】:Converting cURL request command to Coldfusion (cfhttp)将 cURL 请求命令转换为 Coldfusion (cfhttp)
【发布时间】:2016-08-30 22:02:13
【问题描述】:

我正在尝试使用 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 Unauthorized)

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

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

【问题讨论】:

  • 尝试将您的 CURL 按原样发布到 CF 页面并捕获请求的所有变量 - 然后看看您是否可以通过这种方式匹配它们。未知的可能是在标头中传递的用户名。该站点还可能需要不同的代理或其他任何东西......这很难说,但错误消息让我认为某些东西 - 一些令牌或其他东西 - 丢失了。
  • 不应该是username="myCl13nt1D" password="my53cR3tK3Y" in &lt;cfhttp&gt;吗?

标签: curl coldfusion cfhttp


【解决方案1】:

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

<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

【讨论】:

  • 谢谢 Alex/Henry,我不知道是这样的。
猜你喜欢
  • 1970-01-01
  • 2016-05-20
  • 2016-08-04
  • 2018-10-21
  • 2017-05-25
  • 2014-04-12
  • 2021-04-28
  • 2021-12-05
  • 1970-01-01
相关资源
最近更新 更多