我正在尝试使用 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"}
我尝试了不同的方法来传递所需的参数,但它们都返回相同的错误。
最佳答案
-u myCl13nt1D:my53cR3tK3Y
是 BasicAuth并在 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 .
关于curl - 将 cURL 请求命令转换为 Coldfusion (cfhttp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39237768/