Преобразование PHP-кода cUrl в coldfusion

Я пытаюсь преобразовать некоторый код 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);

Холодный синтез Код:

    <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 ошибка.

Я что-то пропустил?

2

Решение

Я что-то пропустил?

  1. Для добавления данных в body из HTTP запрос, вам нужно установить type из cfhttpparam в body,
  2. Вам нужно вручную установить 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

Другие решения

Спасибо @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>
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector