首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >确保Authorization header的值格式正确,包括签名

确保Authorization header的值格式正确,包括签名
EN

Stack Overflow用户
提问于 2021-05-07 22:57:29
回答 1查看 264关注 0票数 1

在使用Azure Storage API访问文件服务时,我们会遇到以下错误。

代码语言:javascript
运行
复制
<?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:5a7f5ef2-a01a-0023-134e-436c77000000 Time:2021-05-07T14:36:32.7067133Z</Message>
  <AuthenticationErrorDetail>Unversioned authenticated access is not allowed.</AuthenticationErrorDetail>
</Error>

我们已经按照文档进行了操作,并且相信我们拥有所有正确的头文件,但显然缺少一些东西。

我们正在编码的签名字符串:

代码语言:javascript
运行
复制
GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:Fri, 07 May 2021 15:29:49 GMT\nx-ms-version:2020-04-08\n/*our_azure_resource*\ncomp:metadata

使用我们用来进行编码的CryptoJS的代码

代码语言:javascript
运行
复制
let signature = CryptoJS.HmacSHA256(CryptoJS.enc.Utf8.parse(stringToSign).toString(), this.key)
                        .toString(CryptoJS.enc.Base64);

Authorization header的值:

代码语言:javascript
运行
复制
SharedKey storageaccountname:decodedstring
EN

回答 1

Stack Overflow用户

发布于 2021-05-10 10:03:40

根据我的测试,我们需要使用以下代码对包crypto-js进行签名

代码语言:javascript
运行
复制
const str = CryptoJS.HmacSHA256(
  inputvalue,
  CryptoJS.enc.Base64.parse(accountKey)
);
const sig = CryptoJS.enc.Base64.stringify(str);

例如

  1. 安装包

代码语言:javascript
运行
复制
npm i crypto-js request xml2js

  1. 代码(列表容器)

代码语言:javascript
运行
复制
var CryptoJS = require("crypto-js");
var request = require("request");
var parseString = require("xml2js").parseString;
const methodName = "GET";
const accountName = "andyprivate";
const accountKey =
  "";

const date = new Date().toUTCString();
const version = "2020-04-08";

const inputvalue =
  methodName +
  "\n" /*VERB*/ +
  "\n" /*Content-Encoding*/ +
  "\n" /*Content-Language*/ +
  "\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*/ +
  "x-ms-date:" +
  date +
  "\n" +
  "x-ms-version:" +
  version +
  "\n" +
  "/" +
  accountName +
  "/" +
  "\ncomp:list";
console.log(inputvalue);
const str = CryptoJS.HmacSHA256(
  inputvalue,
  CryptoJS.enc.Base64.parse(accountKey)
);
const sig = CryptoJS.enc.Base64.stringify(str);

const options = {
  method: "GET",
  url: `https://${accountName}.blob.core.windows.net/?comp=list`,
  headers: {
    "x-ms-date": date,
    "x-ms-version": version,
    Authorization: "SharedKey " + accountName + ":" + sig,
  },
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  parseString(response.body, (error, result) => {
    if (error) throw new Error(error);
    const res = JSON.stringify(result);
    console.log(res);
  });
});

更多详情,请参考here

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67437274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档