我想從我的DocumentDB數據庫中檢索數據,而不使用中間件,因爲我必須將其實施到我的Ionic App中。帶Postman輸出的DocumentDB REST API始終爲「未授權」錯誤
我遵循微軟的this文檔,甚至使用了與本文檔完全相同的代碼(使用Browserify)。
爲了測試連接我使用Postman,在那裏我輸入所有需要的標題。對於master-key
,我使用了DocumentDB的primary-key
。
問題是,從DocumentDB的反應總是如下:
{
"code": "Unauthorized",
"message": "The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'post\ndbs\n\nthu, 29 sep 2016 10:46:49 gmt\n\n'\r\nActivityId: f7ce147d-6ff9-4e4f-aaff-39d3769cc399"
}
我在做什麼錯?
的JS是這樣的:
var Buffer = require('buffer/').Buffer;
var crypto = require("crypto");
var headers = new Array();
headers['x-ms-date'] = "Thu, 29 Sep 2016 17:50:49 GMT";
function getAuthorizationTokenUsingMasterKey(verb, resourceType, resourceId, headers, masterKey) {
var key = new Buffer(masterKey, "base64");
var text = (verb || "").toLowerCase() + "\n" +
(resourceType || "").toLowerCase() + "\n" +
(resourceId || "") + "\n" +
(headers["x-ms-date"] || "").toLowerCase() + "\n" +
"" + "\n";
var body = new Buffer(text, "utf8");
var signature = crypto.createHmac("sha256", key).update(body).digest("base64");
var MasterToken = "master";
var TokenVersion = "1.0";
return "type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature;
}
console.log(getAuthorizationTokenUsingMasterKey("POST", "docs", "test_collection_1", headers, "CJTR8odBZJklUUixWPZDRdTXqJrfLpfhTLk...wO2oPHgPyjuBkbhrjTlvKhRxsAAeig=="));
和我的郵差頭看起來像這樣:
POST request to https://example-database.documents.azure.com/dbs
x-ms-documentdb-isquery: True
x-ms-date: Thu, 29 Sep 2016 17:50:49 GMT
authorization: type%3Dmaster%26ver%3D1.0%26sig%3D3240f1fa7a05...cf845c746bcbb5a1
x-ms-version: 2015-12-16
x-ms-query-enable-crosspartition: true
Accept: application/json
Content-Type: application/query+json
嗨,John,任何更新? –
你有沒有發現如何做到這一點?這裏有類似的問題。 – americanslon