2016-09-29 90 views
1

我想從我的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 
+0

嗨,John,任何更新? –

+0

你有沒有發現如何做到這一點?這裏有類似的問題。 – americanslon

回答

0

你可能只想抓住並從Azure中提供的節點適應的代碼。 DocumentDB的js SDK。這是你想要的文件:https://github.com/Azure/azure-documentdb-node/blob/master/source/lib/auth.js

+0

謝謝,但我使用了這段代碼,並且已經調整它以防止node.js,但它不起作用 –

+0

我現在使用完全相同的代碼從您的鏈接,因爲我安裝了Browserify爲了測試它(我更新了我的問題)。但響應消息始終是相同的 –

+0

Browserify必須具有加密包的polyfil。我想知道這是不同的。嘗試運行node.js中的實際代碼並打印輸出。然後,使用相同的密鑰並在browserified版本或原始版本中打印輸出。 –

0

你應該確認你的'文本'變量是否與'服務器使用下面的有效負載簽名:'之後的錯誤中返回的有效載荷完全匹配。看起來你在負載的「resourceId」部分給這裏的人造成了類似的錯誤:DocumentDB - DELETE causes 401 error。例如,您在上面發佈的示例中,似乎只使用了文檔名稱,而不是完整的資源鏈接。看看我對這個問題的回答 - 它應該解釋正確的方法。

0

根據您的錯誤消息,您的授權簽名與您試圖接近的DocumentDB中的資源不匹配。要使用PostMan來測試連接,您需要針對Document DB的REST API實施正確的Http請求。

E.G.

您可以嘗試通過https://msdn.microsoft.com/en-us/library/azure/mt489065.aspx列出DocumentDB中的DB。

console.log(getAuthorizationTokenUsingMasterKey("GET", "dbs", "", headers, "{your_key}")); 

而且在郵遞員:

通過生成授權頭

GET request to https://example-database.documents.azure.com/dbs 
x-ms-date: Thu, 29 Sep 2016 17:50:49 GMT 
authorization: type%3Dmaster%26ver%3D1.0%26sig%3D.... 
x-ms-version: 2015-08-06 

關於如何產生抗DocumentDB REST API的授權令牌,你可以參考https://msdn.microsoft.com/library/azure/dn783368.aspx瞭解詳情。