将此 cuRL 转换为 CFHTTP - Azure NotificationHub

Convert this cuRL to CFHTTP - Azure NotificationHub

我正在尝试使用 Azure REST API 从 ColdFusion 脚本进行推送。 here 处有一个有效的 PHP 脚本,我已尝试将 PHP 转换为 ColdFusion。下面是我到目前为止的代码,对我来说一切似乎都是正确的,我没有收到任何错误。但是,cfhttp.FileContent 中的结果只是一个空字符串,我没有收到任何通知。谁能指出我的错误?

    <cfset CONNECTION_STRING = "Endpoint=sb://mypushnamespacehere.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=mysharedaccesskeyhere">
    <cfset API_VERSION = "?api-version=2015-01">
    <cfset connectionStringParts = ListToArray(CONNECTION_STRING,';','YES')>
    <cfset ENDPOINT = "https" & #connectionStringParts[1].substring(11)#>
    <cfset HUBPATH = "myhubpathname">
    <cfset sasKEYNAME = #connectionStringParts[2].substring(20)#>
    <cfset sasKEYVALUE = #connectionStringParts[3].substring(16)#>

    <cfset URI = #ENDPOINT# & #HUBPATH# & "/messages" & API_VERSION>

    <cfset TARGET_URI = #Lcase(URLEncodedFormat(URI))#>
    <cfset EXPIRES = int(createObject('java', 'java.lang.System').currentTimeMillis()/1000)>
    <cfset EXPIRES_IN_MINS = 60>
    <cfset EXPIRES = EXPIRES + EXPIRES_IN_MINS * 60>

    <cfset TO_SIGN = #TARGET_URI# & Chr(13) & Chr(10) & #EXPIRES#>
    <cfset SIGNATURE_HMAC = #HMAC(TO_SIGN, sasKEYVALUE, 'HmacSHA256')#>
    <cfset SIGNATURE = #EncodeForURL(binaryEncode(binaryDecode(SIGNATURE_HMAC, 'hex'), 'base64'))#>

    <cfset TOKEN = "SharedAccessSignature sr=" & #TARGET_URI# & "&sig=" & #SIGNATURE# & "&se=" & #EXPIRES# & "&skn=" & #sasKEYNAME#>

    <cftry>
        <!--- Create our requestBody --->
        <cfset requestBody = '{
                                "data" : {
                                    "message" : "Hello from ColdFusion"
                                    }
                                }'>
        <!--- Send Push --->
        <cfhttp method="post" url="#URI#">    
            <cfhttpparam type="header" name="Authorization" value="#TOKEN#">
            <cfhttpparam type="header" name="Content-Type" value="application/json">    
            <cfhttpparam type="header" name="ServiceBusNotification-Format" value="gcm">
            <cfhttpparam type="body" encoded="false" value="#trim(requestBody)#">
        </cfhttp>   
        <!--- Parse JSON Response to determine if call was successful --->
        <cfdump var=#cfHttp.FileContent#>
    <!--- Catch any errors, return false --->
    <cfcatch type="any">
        <cfdump var=#cfcatch#>
    </cfcatch>
    </cftry> 

更新

我使用 PHP 脚本生成的 SIGNATURE 进行了测试,它在 ColdFusion 中有效。所以我生成 SIGNATURE 变量的方式与 PHP 生成它的方式不匹配。

这是 PHP 的作用...

$targetUri = strtolower(rawurlencode(strtolower($uri)));
$expires = time();
$expiresInMins = 60;
$expires = $expires + $expiresInMins * 60;
$toSign = $targetUri . "\n" . $expires;

$signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, $this->sasKeyValue, TRUE)));

$token = "SharedAccessSignature sr=" . $targetUri . "&sig=" . $signature . "&se=" . $expires . "&skn=" . $this->sasKeyName;

想通了。 在 PHP 中,换行符的“\n”工作正常,但当我将其转换为 ColdFusion 时,我使用的是 Chr(13) 和 Chr(10)。把 Chr(13) 拿出来,只用了 Chr(10),现在效果很好。

已更新

<cfset TO_SIGN = TARGET_URI & Chr(13) & Chr(10) & EXPIRES>

<cfset TO_SIGN = TARGET_URI & Chr(10) & EXPIRES>