我试图将一些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
错误。
我错过了什么吗?
答案 0 :(得分:1)
我错过了什么吗?
body
请求的HTTP
,您需要将type
cfhttpparam
设置为body
。Content-Type
标题。所以,你可以试试这个:
<!--- Set defaults --->
<cfset requestBody = "">
<cfset stsUrl = "https://login.windows.net/#appTenantDomainName#/oauth2/token?api-version=1.0">
<!--- Set Data Variables --->
<cfset data["grant_type"] = "client_credentials">
<cfset data["client_secret"] = clientSecret>
<cfset data["resource"] = graphPrincipalId>
<cfset data["client_id"] = clientPrincipalId>
<!--- Request Body --->
<cfloop collection="#data#" item="key">
<cfset requestBody &= key & "=" & data[key] & "&">
</cfloop>
<cfset requestBody = reReplace(requestBody, "&$", "", "1")>
<!--- Request --->
<cfhttp url="#stsUrl#" method="POST" result="resultName">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="body" value="#requestBody#">
</cfhttp>
与问题无关:您必须正确调整变量范围。
答案 1 :(得分:0)
感谢@Beginner指导我完成。以下解决方案有效:
<cfset authenticationRequestBody = "grant_type=client_credentials&client_secret=#clientSecret#&resource=#graphPrincipalId#&client_id=#clientPrincipalId#">
<cfset stsUrl = "https://login.windows.net/#appTenantDomainName#/oauth2/token?api-version=1.0">
<cfhttp url="#stsUrl#" method="POST" result="resultName">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="body" value="#authenticationRequestBody#">
</cfhttp>