无法授权查询 Azure Table 服务
Cannot authorize query to Azure Table Service
我正在尝试设置一个 API 以从 Azure Table 存储中获取信息。我一直在关注文档 here and a similar issue on SO ,但我无法让它工作。我收到 HTTP 403 错误 "Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."。这是代码。现在我只是尝试对 /Tables 资源进行基本查询,尽管最终我将查询特定的 rowIDs。
const crypto = require("crypto");
const request = require('request-promise-native');
const yourStorageAccountName = 'XXXXXXXX';
let CanonicalizedResource = `${yourStorageAccountName}/Tables`;
let url = `https://${yourStorageAccountName}.table.core.windows.net/Tables`;
let now = new Date();
let nowUTC = now.toUTCString();
let stringToSign = `GET\n\n\n${nowUTC}\n${CanonicalizedResource}`;
let accesskey = `YYYYYYYY`;
let key = new Buffer(accesskey, "base64");
let hmac = crypto.createHmac("sha256", key);
hmac.update(stringToSign);
let sig = hmac.digest("base64");
console.log("SIGNATURE : " + sig);
console.log("nowutc : " + nowUTC);
let headers = {
"Authorization": "SharedKey " + yourStorageAccountName + ":" + sig,
"x-ms-date": nowUTC,
"Date": nowUTC,
"x-ms-version": "2015-12-11"
};
var response = request({
url: url,
method: 'GET',
headers: headers
});
console.log(response);
根据我的测试,我们可以使用下面的代码来创建share key
const accesskey=""
const storageAccount = ""
const resource = "Tables"
const data = new Date(Date.UTC(2020, 1, 2, 3, 4, 5));
const GMTTime = data.toUTCString()
console.log(GMTTime)
//Shared Key authorization
const StringToSign= "GET"+"\n"
+"\n"
+"\n"
+ GMTTime +"\n"
+"/"+ storageAccount+"/"+resource
const Sig = crypto.createHmac('sha256', Buffer.from(accesskey, 'base64')).update(StringToSign, 'utf8').digest('base64');
console.log(Sig)
//Shared Key Lite authorization
const stringToSign = GMTTime +"\n"+"/"+ storageAccount+"/"+resource
const sig = crypto.createHmac('sha256', Buffer.from(accesskey, 'base64')).update(stringToSign, 'utf8').digest('base64');
console.log(sig)
测试(Query tables)
1.共享密钥授权
GET https://myaccount.table.core.windows.net/Tables
Headers
Authorization : SharedKey <account name>:kHl5K0AzsG7M32***AoxmCFY=
x-ms-date : <the data you use to create share key>
Accept : application/json;odata=nometadata
x-ms-version : 2017-04-17
- Shared Key Lite 授权
GET https://myaccount.table.core.windows.net/Tables
Headers
Authorization : SharedKeyLite <account name>:0fADhBTi7tvtm***h69Y433c=
x-ms-date : <the data you use to create share key>
Accept : application/json;odata=nometadata
x-ms-version : 2017-04-17
此外,如果你想使用Azure Table nodejs sdk azure-storage
,请参考document and the sample
- 安装sdk
npm install azure-storage
- 代码
var azure=require('azure-storage')
async function main() {
const accesskey=""
const storageAccount = ""
var tableService=azure.createTableService(storageAccount,accesskey);
tableService.listTablesSegmented(null,function(error,result){
if(error){
console.log(error)
}else{
for (var i = 0, table; table = result.entries[i]; i++) {
console.log(table)
}
}
})
}
// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed the sample.");
})
.catch((err) => {
console.log(err.message);
});
我正在尝试设置一个 API 以从 Azure Table 存储中获取信息。我一直在关注文档 here and a similar issue on SO
const crypto = require("crypto");
const request = require('request-promise-native');
const yourStorageAccountName = 'XXXXXXXX';
let CanonicalizedResource = `${yourStorageAccountName}/Tables`;
let url = `https://${yourStorageAccountName}.table.core.windows.net/Tables`;
let now = new Date();
let nowUTC = now.toUTCString();
let stringToSign = `GET\n\n\n${nowUTC}\n${CanonicalizedResource}`;
let accesskey = `YYYYYYYY`;
let key = new Buffer(accesskey, "base64");
let hmac = crypto.createHmac("sha256", key);
hmac.update(stringToSign);
let sig = hmac.digest("base64");
console.log("SIGNATURE : " + sig);
console.log("nowutc : " + nowUTC);
let headers = {
"Authorization": "SharedKey " + yourStorageAccountName + ":" + sig,
"x-ms-date": nowUTC,
"Date": nowUTC,
"x-ms-version": "2015-12-11"
};
var response = request({
url: url,
method: 'GET',
headers: headers
});
console.log(response);
根据我的测试,我们可以使用下面的代码来创建share key
const accesskey=""
const storageAccount = ""
const resource = "Tables"
const data = new Date(Date.UTC(2020, 1, 2, 3, 4, 5));
const GMTTime = data.toUTCString()
console.log(GMTTime)
//Shared Key authorization
const StringToSign= "GET"+"\n"
+"\n"
+"\n"
+ GMTTime +"\n"
+"/"+ storageAccount+"/"+resource
const Sig = crypto.createHmac('sha256', Buffer.from(accesskey, 'base64')).update(StringToSign, 'utf8').digest('base64');
console.log(Sig)
//Shared Key Lite authorization
const stringToSign = GMTTime +"\n"+"/"+ storageAccount+"/"+resource
const sig = crypto.createHmac('sha256', Buffer.from(accesskey, 'base64')).update(stringToSign, 'utf8').digest('base64');
console.log(sig)
测试(Query tables) 1.共享密钥授权
GET https://myaccount.table.core.windows.net/Tables
Headers
Authorization : SharedKey <account name>:kHl5K0AzsG7M32***AoxmCFY=
x-ms-date : <the data you use to create share key>
Accept : application/json;odata=nometadata
x-ms-version : 2017-04-17
- Shared Key Lite 授权
GET https://myaccount.table.core.windows.net/Tables
Headers
Authorization : SharedKeyLite <account name>:0fADhBTi7tvtm***h69Y433c=
x-ms-date : <the data you use to create share key>
Accept : application/json;odata=nometadata
x-ms-version : 2017-04-17
此外,如果你想使用Azure Table nodejs sdk azure-storage
,请参考document and the sample
- 安装sdk
npm install azure-storage
- 代码
var azure=require('azure-storage')
async function main() {
const accesskey=""
const storageAccount = ""
var tableService=azure.createTableService(storageAccount,accesskey);
tableService.listTablesSegmented(null,function(error,result){
if(error){
console.log(error)
}else{
for (var i = 0, table; table = result.entries[i]; i++) {
console.log(table)
}
}
})
}
// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed the sample.");
})
.catch((err) => {
console.log(err.message);
});