将cUrl-PHP代码转换为coldfusion

将cUrl-PHP代码转换为coldfusion,php,curl,coldfusion,coldfusion-9,cfhttp,Php,Curl,Coldfusion,Coldfusion 9,Cfhttp,我试图将一些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'; $prote

我试图将一些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错误请求
错误


我错过了什么吗?

感谢@初学者的指导。以下解决方案奏效了:

<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>

我错过什么了吗

  • 要将数据添加到
    HTTP
    请求的
    正文
    ,需要将
    类型
    设置为
    正文
  • 您需要手动设置正文中内容类型的
    内容类型
    标题
  • 因此,您可以尝试以下方法:

    <!--- 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>
    
    
    
    与问题无关:您必须正确确定变量的范围


    您如何设置authenticationRequestBody?你能在你的帖子里加上吗?另外,要向请求添加数据,正文类型必须是
    body
    @初学者:添加了更多详细信息。因此,您认为httpparams的类型应该是
    body
    而不是
    formified
    httpparams的类型应该是body而不是formified?
    您试图将数据添加到请求body。所以它应该是
    body
    ,您需要手动设置
    内容类型
    标题。文档:。感谢您发布工作代码。然而,如果@初学者提供了解决方案,请他们将自己的评论提升为官方的“答案”(并将其标记为解决方案),作为对帮助的感谢,这可能是一个很好的姿态:-)@Leigh:初学者仍然可以将其评论作为答案发布。我已经对他的评论投了赞成票,并在我的工作解决方案中提到了他。{最好的鲍勃·巴克印象是什么?}@初学者下来吧…;-)