gpt4 book ai didi

javascript - Azure 存储身份验证 (JavaScript)

转载 作者:行者123 更新时间:2023-11-28 05:26:20 27 4
gpt4 key购买 nike

我一直在查看 MSDN 文档、文章和堆栈溢出,但不确定问题是什么。

https://www.kaizenspark.com/blog/topic/mirthconnect

我一直在使用本文来尝试使用 Mirth Connect 连接到 Azure 队列存储。

Azure 授权是这样完成的:

importPackage(javax.crypto);
importPackage(javax.crypto.spec);
importPackage(org.apache.commons.codec.binary);

// Change the variables below to your actual account details
// Remember the queue name must be lower case or it will fail

var account = "devstoreaccount1";
var key = "Access key";
var path = "devstoreaccount1/myqueuename/messages";
// No changes below this line
var apiVersion = "2011-08-18";
var contentLength = String(tmp).length;
var gmtDateString = new Date().toGMTString();

var stringToSign =
"POST\n" + /*HTTP Verb*/
"\n" + /*Content-Encoding*/
"\n" + /*Content-Language*/
contentLength + "\n" + /*Content-Length*/
"\n" + /*Content-MD5*/
"text/xml; charset=UTF-8\n" + /*Content-Type*/
"\n" + /*Date*/
"\n" + /*If-Modified-Since */
"\n" + /*If-Match*/
"\n" + /*If-None-Match*/
"\n" + /*If-Unmodified-Since*/
"\n" + /*Range*/
/* CanonicalizedHeaders */
"x-ms-date:" + gmtDateString + "\n" +
"x-ms-version:" + apiVersion + "\n" +
/* CanonicalizedResource */
"/" + account + "/" + path;


var mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(Base64.decodeBase64(key), mac.getAlgorithm()));
mac.update(java.lang.String(stringToSign).getBytes("UTF-8"));
var hmac = Base64.encodeBase64String(mac.doFinal());
connectorMap.put('Authorization', 'SharedKey ' + account + ':' + hmac);
connectorMap.put('x-ms-date', gmtDateString);
connectorMap.put('x-ms-version', apiVersion);
connectorMap.put('path', path);

除了输入我自己的帐户名、 key 和路径之外,我没有更改任何内容。根据此处的文档:https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx# , stringToSign 似乎是正确的。

但是,我收到此错误:

ERROR MESSAGE: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:'id' Time:2016-10-19T17:24:51.6491513Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request 'key' is not the same as any computed signature. Server used following string to sign: 'POST 88 text/xml; charset=UTF-8 x-ms-date:Wed, 19 Oct 2016 17:24:50 GMT x-ms-version:2011-08-18 /accountname/queuename'.</AuthenticationErrorDetail></Error>

出于隐私考虑,我将请求 ID 更改为“ key ”、帐户名称和队列名称,但除此之外,错误消息与显示给我的一样。任何帮助将不胜感激!

最佳答案

根据您的错误消息:

Server used following string to sign: 'POST 88 text/xml; charset=UTF-8 x-ms-date:Wed, 19 Oct 2016 17:24:50 GMT x-ms-version:2011-08-18 /accountname/queuename'

您的 SDK 在生成签名时似乎出现问题。 CanonicalizedResource应该像 /<accountname>/<queuename>/messages ,现在缺少 /messages在你的签名中。

Put Message请引用以下纯node.js代码:

var crypto = require('crypto');
var request = require('request');
var key = "<key>";
var strTime = (new Date()).toGMTString();
var apiVersion = "2011-08-18";
var account = "<accountname>";
var path = "myqueue/messages";
var body = "<QueueMessage>"+
"<MessageText>PHNhbXBsZT5zYW1wbGUgbWVzc2FnZTwvc2FtcGxlPg==</MessageText>"+
"</QueueMessage>";
var strToSign =
"POST\n" + /*HTTP Verb*/
"\n" + /*Content-Encoding*/
"\n" + /*Content-Language*/
body.length + "\n" + /*Content-Length*/
"\n" + /*Content-MD5*/
"\n" + /*Content-Type*/
"\n" + /*Date*/
"\n" + /*If-Modified-Since */
"\n" + /*If-Match*/
"\n" + /*If-None-Match*/
"\n" + /*If-Unmodified-Since*/
"\n" + /*Range*/
/* CanonicalizedHeaders */
"x-ms-date:" + strTime + "\n" +
"x-ms-version:" + apiVersion + "\n" +
/* CanonicalizedResource */
"/" + account + "/" + path;
var sharedKey = crypto.createHmac('sha256',new Buffer(key,'base64')).update(strToSign, 'utf-8').digest('base64');
var auth = "SharedKey "+account+":"+sharedKey;
var options = {
method: 'POST',
url: 'https://'+account+'.queue.core.windows.net/'+path,
headers: {
'Authorization':auth,
'x-ms-date':strTime,
'x-ms-version':apiVersion
},
body: body
};
request(options, function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info);
}else{
// console.log(response)
console.log(body)
}
});

关于javascript - Azure 存储身份验证 (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40138120/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com