node.js - 文档数据库 : Coldfusion Returning 401 Authorization error

标签 node.js azure coldfusion azure-cosmosdb

我尝试通过 Coldfusion 连接 Azure DocumentDB/CosmosDB,但收到 401 授权错误。我已经安装了证书,但仍然遇到同样的问题。我尝试过 PHP、Nodejs,除了 ColdFusion 之外,两者都表现良好。还尝试使用 Coldfusion 脚本,但错误仍然相同。这是我写的代码:

<cfset x_ms_date="#GetHttpTimeString(now())#"/>
<cfset br = "#chr(13)##chr(10)#">
<cfset signStr="GET#br#dbs#br##br##x_ms_date##br##br#" />
<cfset strBase64ValueKey="BASE64_ENCODED_MASTER_KEY" />
<cfset key=ToString(ToBinary(strBase64ValueKey))/>
<cfset x=ToBase64(hmac(LCase(signStr),LCase(key),"HMACSHA256"))>


<cfhttp method="GET" url="https://APP_URL.documents.azure.com/dbs" throwonerror="Yes">
<cfhttpparam name="Authorization" type="header" value="#URLEncodedFormat("type=master&ver=1.0&sig="&x)#">
<cfhttpparam name="x-ms-date" type="header" value="#x_ms_date#">
<cfhttpparam name="x-ms-version" type="header" value="2017-02-22">
</cfhttp>
<cfoutput>
    #cfhttp.fileContent#
</cfoutput>

以下是上述代码的响应:

{"code":"Unauthorized","message":"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get\ndbs\n\nthu, 08 feb 2018 19:52:03 gmt\n\n'\r\nActivityId: aab94428-63a0-4eb7-807b-SOMETHING, Microsoft.Azure.Documents.Common/1.20.186.1"}

这段代码有什么问题?提前致谢

最佳答案

如果您将代码更改为如下所示,这将起作用:

<cfset x_ms_date = GetHttpTimeString(now()) />

<cfset strBase64ValueKey = "BASE64_ENCODED_MASTER_KEY" />
<cfset br = chr(10)>
<cfset signStr = "GET#br#dbs#br##br##x_ms_date##br##br#" />

<cfset secret = createObject('java', 'javax.crypto.spec.SecretKeySpec' ).Init(BinaryDecode(strBase64ValueKey, "Base64"), 'HmacSHA256')/>
<cfset mac = createObject('java', "javax.crypto.Mac")/>
<cfset mac = mac.getInstance("HmacSHA256")/>
<cfset mac.init(secret)/>
<cfset x = mac.doFinal(LCase(signStr).GetBytes())>
<cfset sig = BinaryEncode(x, "Base64")>
<cfset token = URLEncodedFormat("type=master&ver=1.0&sig=#sig#")>


<cfhttp method="GET" url="https://APP_URL.documents.azure.com/dbs" throwonerror="Yes">
    <cfhttpparam name="Authorization" type="header" value="#token#">
    <cfhttpparam name="x-ms-date" type="header" value="#x_ms_date#">
    <cfhttpparam name="x-ms-version" type="header" value="2017-02-22">
</cfhttp>

<cfoutput>
    #cfhttp.fileContent#
</cfoutput>

关于node.js - 文档数据库 : Coldfusion Returning 401 Authorization error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693967/

相关文章:

postgresql - Azure数据工厂: Which Version of npgsql is used?

php - 使用 CFHTTP 将 HTML 表单数组提交给 PHP

java - 将 ColdFusion SMS 网关绑定(bind)到特定 IP 地址

javascript - 在 Node Js 中从字符串转换为 DateTime

node.js - 快速 Passport 检查用户是否经过身份验证

azure - 在 Web 应用程序中将不同的文件类型全部作为应用程序/八位字节流提供服务有何影响?

coldfusion - 在 ColdFusion 中使用重定向的 Web 服务

javascript - 如何查看在 Zeit Docker/Node 容器中创建的文件

javascript - 处理错误后跳过 promise 链

asp.net - FileSystemWatcher 在 Azure 中不会触发