【问题标题】:Azure Storage Authentication (JavaScript)Azure 存储身份验证 (JavaScript)
【发布时间】:2017-03-01 11:47:00
【问题描述】:

我一直在查看 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);

除了输入我自己的帐户名、密钥和路径外,我没有进行任何更改。 根据此处的文档: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 更改为“密钥”和帐户名称以及队列名称,否则错误消息与显示给我的相同。任何帮助将不胜感激!

【问题讨论】:

  • 你能说出你想做什么操作吗?看起来您正在尝试将消息添加到队列中,但想确定一下。另外,请确认您尝试使用存储模拟器而不是云存储帐户来执行此操作。

标签: javascript azure authentication azure-storage mirth


【解决方案1】:

根据您的错误信息:

服务器使用以下字符串签名:'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 应该类似于 /&lt;accountname&gt;/&lt;queuename&gt;/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)
  }
});

【讨论】:

    猜你喜欢
    • 2017-10-30
    • 2021-12-17
    • 2021-07-30
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2012-06-06
    • 2019-01-06
    相关资源
    最近更新 更多