【问题标题】:How do I Post messages to Azure Queue Storage using Postman?如何使用 Postman 将消息发布到 Azure 队列存储?
【发布时间】:2022-03-04 02:32:59
【问题描述】:

我需要在 Postman 应用程序中完成这一切。我找到了使用 blob 和表的示例,但它们似乎不适合队列存储。我认为最大的问题是我无法正确创建签名部分。

我相信我设法在邮递员中执行了 Get Message,但我得到“服务器无法验证请求”。 Post 请求时出错。我尝试发布的消息是 XML 格式,并使用与 Get 相同的预请求脚本。

Post 和 Get 请求的预请求脚本(从在线资源复制):

const storageAccount = pm.variables.get('azure_storage_account');
const accountKey = pm.variables.get('azure_storage_key');

pm.variables.set("header_date", new Date().toUTCString());

// Get hash of all header-name:value
const headers = pm.request.getHeaders({ ignoreCase: true, enabled: true });

// Construct Signature value for Authorization header
var signatureParts = [
    pm.request.method.toUpperCase(),
    headers["content-encoding"] || "",
    headers["content-language"] || "",
    headers["content-length"]  || "",
//    pm.request.body ? pm.request.body.toString().length || "" : "",
    headers["content-md5"] || "",
    headers["content-type"] || "",
    headers["x-ms-date"] ? "" : (pm.variables.get("header_date") || ""),
    headers["if-modified-since"] || "",
    headers["if-match"] || "",
    headers["if-none-match"] || "",
    headers["if-unmodified-since"] || "",
    headers["range"] || ""
];

// Construct CanonicalizedHeaders
const canonicalHeaderNames = [];
Object.keys(headers).forEach(key => {
    if (key.startsWith("x-ms-")) {
        canonicalHeaderNames.push(key);
    }
});
// Sort headers lexographically by name
canonicalHeaderNames.sort();

const canonicalHeaderParts = [];
canonicalHeaderNames.forEach(key => {
    let value = pm.request.getHeaders({ ignoreCase: true, enabled: true })[key];

    // Populate variables
    value = pm.variables.replaceIn(value);

    // Replace whitespace in value but not if its within quotes
    if (!value.startsWith("\"")) {
        value = value.replace(/\s+/, " ");
    }

    canonicalHeaderParts.push(`${key}:${value}`);
});

// Add headers to signature
signatureParts.push.apply(signatureParts, canonicalHeaderParts);

// Construct CanonicalizedResource
const canonicalResourceParts = [
    `/${pm.variables.get("azure_storage_account")}${pm.request.url.getPath()}`
];
const canonicalQueryNames = [];
pm.request.url.query.each(query => {
    canonicalQueryNames.push(query.key.toLowerCase());
});
canonicalQueryNames.sort();
canonicalQueryNames.forEach(queryName => {
    const value = pm.request.url.query.get(queryName);

    // NOTE: This does not properly explode multiple same query params' values
    // and turn them into comma-separated list
    canonicalResourceParts.push(`${queryName}:${value}`);
});
// Add resource to signature
signatureParts.push.apply(signatureParts, canonicalResourceParts);

console.log("Signature Parts", signatureParts);

// Now, construct signature raw string
const signatureRaw = signatureParts.join("\n");

console.log("Signature String", JSON.stringify(signatureRaw));

// Hash it using HMAC-SHA256 and then encode using base64
const storageKey = pm.variables.get("azure_storage_key");
const signatureBytes = CryptoJS.HmacSHA256(signatureRaw, CryptoJS.enc.Base64.parse(storageKey));
const signatureEncoded = signatureBytes.toString(CryptoJS.enc.Base64);

console.log("Storage Account", pm.variables.get("azure_storage_account"));
console.log("Storage Key", storageKey);


// Finally, make it available for headers
pm.variables.set("header_authorization",
    `SharedKey ${pm.variables.get("azure_storage_account")}:${signatureEncoded}`);

邮递员中的标题: Headers and URI of Request

有没有人有工作示例或知道从这里移到哪里?

编辑: 添加控制台和错误图像:

Console output censored for safety

Error output censored for safety

【问题讨论】:

  • 你能分享完整的错误信息和console.log输出吗?
  • 既然你有账户密钥,你可以在没有签名的情况下完成请求,只需使用 SharedKey auth:docs.microsoft.com/en-us/rest/api/storageservices/put-message
  • 您能否编辑您的问题并包含SignatureString 的文本版本和错误消息?无论您在屏幕截图中隐藏了什么(例如帐户名称),请将它们替换为 xxxx。

标签: azure postman azure-storage azure-storage-queues


【解决方案1】:

正如您所说,它适用于 Get 如果使用 SAS,则可能是权限问题。确保存储队列上的 Storage-Queue-Contributor 权限。

如果使用 SAS,这个错误可能有很多原因:

  1. 检查您是否没有足够的 SAS 权限。检查您是否标记了读取、写入、列出权限。或者尝试生成新的 SAS 密钥。
  2. 确保请求在以编程方式访问时不包含任何空标头。如果特定标头的值为空(或 null),则应从请求中排除该标头。
  3. 如果不是以上原因,请尝试升级sdks版本 4.请检查编码是否正确,例如:UTF-8。

Azure Storage Queue via REST API c# using Shared Key Authentication - Stack Overflow

参考资料:

  1. c# - The MAC signature found in the HTTP request ' ' is not the same as any computed signature. Server used following string to sign: 'PUT - Stack Overflow
  2. Authorize with Shared Key (REST API) - Azure Storage | Microsoft Docs
  3. rest - The MAC signature found in the HTTP request '...' is not the same as any computed signature - Stack Overflow

【讨论】:

    猜你喜欢
    • 2019-11-08
    • 2021-02-22
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多