I want to retrieve data from my DocumentDB database without a middleware, because I have to implement this into my Ionic App.
I followed this documentation from Microsoft, and even used the exact same code from this documentation (with Browserify).
In order to test the connection I use Postman, where I input all the required headers. For the master-key
I used the primary-key
from DocumentDB.
Problem is, that the response from the DocumentDB is always the following:
{
"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"
}
What am I doing wrong?
The JS looks like this:
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=="));
And my headers in Postman looks like that:
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