具有Postman输出的DocumentDB REST API始终是“未授权”的。错误 [英] DocumentDB REST API with Postman outputs always "Unauthorized" error

查看:173
本文介绍了具有Postman输出的DocumentDB REST API始终是“未授权”的。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的DocumentDB数据库中检索数据没有中间件,因为我必须将其实现到我的Ionic App中。

I want to retrieve data from my DocumentDB database without a middleware, because I have to implement this into my Ionic App.

I遵循Microsoft的文档,以及甚至使用了本文档中完全相同的代码(使用 Browserify )。

I followed this documentation from Microsoft, and even used the exact same code from this documentation (with Browserify).

为了测试连接我使用Postman,我输入了所有必需的标题。对于主密钥,我使用了DocumentDB中的主键

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.

问题是,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"
}

我做错了什么?

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=="));

我在Postman中的标题看起来像这样:

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


推荐答案

根据您的错误消息,您的授权签名与您尝试的DocumentDB中的资源不匹配接近。要使用PostMan测试连接,您需要针对Document DB的REST API实现正确的Http请求。

According your error message, your authorization signature didn't match the resource in DocumentDB you attempted to approach. To use PostMan to test the connection, you need to implement a correct Http request against the REST APIs of Document DB.

EG

您可以尝试通过 https://msdn.microsoft.com/en-us/library/azure/mt489065.aspx

通过以下方式生成授权标头:

Generate the authorization header via:

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

在PostMan中:

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 了解详情。

About how to generate the authorization token against DocumentDB Rest APIs, you can refer to https://msdn.microsoft.com/library/azure/dn783368.aspx for details.

这篇关于具有Postman输出的DocumentDB REST API始终是“未授权”的。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆