Reputation: 1009
I'm trying to convert some PHP code into ColdFusion and having issues in the curl method.
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 Code:
<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>
When running the cfhttp call, I'm getting the 400 Bad Request
error.
Am I missing something?
Upvotes: 2
Views: 587
Reputation: 6236
Am I missing something?
body
of a HTTP
request, you need to set type
of cfhttpparam
to body
.Content-Type
header for the type of content in body.So, you can try this:
<!--- 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>
Not related to the question : You must properly scope your variables.
Upvotes: 1
Reputation: 1009
Thanks to @Beginner for guiding me through. The following solution worked:
<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>
Upvotes: 0