【发布时间】:2015-12-07 05:51:40
【问题描述】:
我正在尝试将一些 PHP 代码转换为 ColdFusion 并在 curl 方法中遇到问题。
PHP:
// Password
$clientSecret = urlencode(settings::$password);
// Information about the resource we need access for which in this case is graph.
$graphId = 'https://graph.windows.net';
$protectedResourceHostName = 'graph.windows.net';
$graphPrincipalId = urlencode($graphId);
// Information about the app
$clientPrincipalId = urlencode($appPrincipalId);
// Construct the body for the STS request
$authenticationRequestBody = 'grant_type=client_credentials&client_secret='.$clientSecret
.'&'.'resource='.$graphPrincipalId.'&'.'client_id='.$clientPrincipalId;
//Using curl to post the information to STS and get back the authentication response
$ch = curl_init();
// set url
$stsUrl = 'https://login.windows.net/'.$appTenantDomainName.'/oauth2/token?api-version=1.0';
curl_setopt($ch, CURLOPT_URL, $stsUrl);
// Get the response back as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Mark as Post request
curl_setopt($ch, CURLOPT_POST, 1);
// Set the parameters for the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $authenticationRequestBody);
// By default, HTTPS does not work with curl.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// read the output from the post request
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
ColdFusion代码:
<cfhttp url="#stsUrl#" method="POST" result="resultName">
<cfhttpparam type="formfield" name="grant_type" value="client_credentials">
<cfhttpparam type="formfield" name="client_secret" value="#clientSecret#">
<cfhttpparam type="formfield" name="resource" value="#graphPrincipalId#">
<cfhttpparam type="formfield" name="client_id" value="#clientPrincipalId#">
</cfhttp>
运行 cfhttp 调用时,我收到了 400 Bad Request 错误。
我错过了什么吗?
【问题讨论】:
-
你如何设置
authenticationRequestBody?你可以在你的帖子中添加它吗?另外,要添加数据到请求正文类型必须是body。 -
@Beginner :添加了更多详细信息。所以你认为 httpparams 需要是
body类型而不是formfied? -
httpparams need to be of type body instead of formfied?您正在尝试将数据添加到请求正文。所以它应该是body并且您需要为此手动设置content-type标头。文档:cfhttpparam.
标签: php curl coldfusion coldfusion-9 cfhttp