gpt4 book ai didi

azure - DocumentDB REST API - 授权 token 错误

转载 作者:行者123 更新时间:2023-12-05 00:14:55 26 4
gpt4 key购买 nike

问题

每当我们请求列表或查询时,我们都会看到 DocumentDB REST API 返回此错误,但当我们按名称/ID 获取对象时则不会:

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.

背景

我们已经成功地将 Node.js sdk 与 DocumentDB 结合使用一年多了,但由于我们希望将后端 Restful API 代码从 Node.js 应用服务迁移到 Azure Functions,我们看到 10-30第二个延迟时间开始发挥作用,因为当函数有一段时间没有被调用时,DocumentDB sdk 加载缓慢。根据之前与 Azure Functions 团队的沟通,我们知道 Function 实例是热实例,而不是冷实例问题。

为了解决这个问题,我们想要测试 DocumentDB REST API,它需要零个外部库才能在 node.js 函数中运行,并且应该尽快执行。

代码

这是在本地 Node.js 中运行的测试工具。一旦它运行,我们会将其移至 Azure 函数。

var express = require('express');
var router = express.Router();
var crypto = require("crypto");
var request = require('request');

router.get('/', function (req, res, next) {

var key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var uri = "https://xxxxxx.documents.azure.com";

var verb = 'GET';
var type = 'dbs';
var link = 'dbs';
var url = `${uri}/${link}`;

var headers = getDefaultRequestHeaders();

// var body = `{"query":"SELECT * FROM c", "parameters": []}`;
var body = '';

headers['content-length'] = body.length;
headers['authorization'] = getAuthorizationTokenUsingMasterKey(verb, type, link, headers['x-ms-date'], key);

request[verb.toLowerCase()]({"url": url, "headers": headers, "body": body}, function (error, response, body) {

// console.log(`error is ${error}`);
// console.log(`response is ${JSON.stringify(response, null, 2)}`);
console.log(`body is ${body}`);

res.status(response.statusCode).json(body);

});

});

function getDefaultRequestHeaders(isQuery, date) {

var headers = {
"content-type": "application/json",
"x-ms-date": new Date().toUTCString(),
"x-ms-version": "2017-02-22",
"accept": "application/json",
"cache-control": "no-cache",
"user-agent": "xxxxxx/1.0"
};

if(isQuery) {
headers["x-ms-documentdb-isquery"] = true;
headers["content-type"] = "application/query+json";
}

if(date) {
headers["x-ms-date"] = date;
}

return headers;

}

function getAuthorizationTokenUsingMasterKey(verb, resourceType, resourceLink, date, masterKey) {
var key = new Buffer(masterKey, "base64");

var text = (verb || "").toLowerCase() + "\n" +
(resourceType || "").toLowerCase() + "\n" +
(resourceLink || "") + "\n" +
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 encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);
}


module.exports = router;

我们正在逐字使用 Access control in the DocumentDB API 中的 getAuthorizationTokenFromMasterKey 函数。页。

为了隐私/安全, key 、应用程序名称和用户代理已替换为 x。

测试结果

列出数据库

当我尝试最基本的调用 list dbs 时服务器返回 token 错误:

  var verb = 'GET';
var type = 'dbs';
var link = 'dbs';

回应:

"{\"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: 'get\ndbs\n\nsat, 12 aug 2017 12:28:41 gmt\n\n'\r\nActivityId: acbf19d9-6485-45c5-9c30-6aa21f14d5b3\"}"

获取数据库

但是,当我执行get database时要求它工作正常:

  var verb = 'GET';
var type = 'dbs';
var link = 'dbs/00001';

回应:

"{\"id\":\"00001\",\"_rid\":\"0eUiAA==\",\"_ts\":1441256154,\"_self\":\"dbs\/0eUiAA==\/\",\"_etag\":\"\\"00007d4a-0000-0000-0000-55e7d2da0000\\"\",\"_colls\":\"colls\/\",\"_users\":\"users\/\"}"

列出集合

同样,请求 list of collections从该数据库返回一个 token 错误:

var verb = 'GET';
var type = 'colls';
var link = 'dbs/00001/colls';

回复:

"{\"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: 'get\ncolls\ndbs/00001\nsat, 12 aug 2017 12:32:19 gmt\n\n'\r\nActivityId: 8a9d4ff8-24ef-4fd2-b400-f9f8aa743572\"}"

获取收​​藏

但是当我打电话get collection时我得到了有效的回复:

var verb = 'GET';
var type = 'colls';
var link = 'dbs/00001/colls/00001';

回应:

"{\"id\":\"00001\",\"indexingPolicy\":{\"indexingMode\":\"consistent\",\"automatic\":true,\"includedPaths\":[{\"path\":\"\/*\",\"indexes\":[{\"kind\":\"Range\",\"dataType\":\"Number\",\"precision\":-1},{\"kind\":\"Range\",\"dataType\":\"String\",\"precision\":-1},{\"kind\":\"Spatial\",\"dataType\":\"Point\"}]}],\"excludedPaths\":[]},\"_rid\":\"0eUiAJMAdQA=\",\"_ts\":1454200014,\"_self\":\"dbs\/0eUiAA==\/colls\/0eUiAJMAdQA=\/\",\"_etag\":\"\\"00000100-0000-0000-0000-56ad54ce0000\\"\",\"_docs\":\"docs\/\",\"_sprocs\":\"sprocs\/\",\"_triggers\":\"triggers\/\",\"_udfs\":\"udfs\/\",\"_conflicts\":\"conflicts\/\"}"

列出文档

请求list documents该集合给了我这个错误:

var verb = 'GET';
var type = 'docs';
var link = 'dbs/00001/colls/00001/docs';

回应:

"{\"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: 'get\ndocs\ndbs/00001/colls/00001\nsat, 12 aug 2017 12:34:48 gmt\n\n'\r\nActivityId: 57097e95-c41b-4770-b91a-370418ef2cce\"}"

获取文档

毫不奇怪,获取 single document工作正常:

var verb = 'GET';
var type = 'docs';
var link = 'dbs/00001/colls/00001/docs/e7fe638d-2152-2097-f9c6-9801d7cf5cdd';

回应:

"{\"name\":\"test rest api\",\"id\":\"e7fe638d-2152-2097-f9c6-9801d7cf5cdd\",\"_rid\":\"0eUiAJMAdQCbHgAAAAAAAA==\",\"_self\":\"dbs\/0eUiAA==\/colls\/0eUiAJMAdQA=\/docs\/0eUiAJMAdQCbHgAAAAAAAA==\/\",\"_etag\":\"\\"0d00d1ee-0000-0000-0000-598ef7d40000\\"\",\"_attachments\":\"attachments\/\",\"_ts\":1502541779}"

查询文档

最后,发送query也会导致 token 错误:

var verb = 'POST';
var type = 'docs';
var link = 'dbs/00001/colls/00001/docs';
var body = `{"query":"SELECT * FROM c", "parameters": []}`;

回应:

"{\"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\ndocs\ndbs/00001/colls/00001\nsat, 12 aug 2017 12:35:42 gmt\n\n'\r\nActivityId: b8b95f8c-1339-423e-b0e7-0d15d3056180\"}"

最佳答案

我认为文档不正确。在他们说resourceLink的地方,他们实际上应该说resource id。如果您查看 Node SDK 代码,就会发现它们计算授权 header 的方式如下(注意 resourceId 的使用):

getAuthorizationTokenUsingMasterKey: function (verb, resourceId, resourceType, headers, masterKey) {
var key = new Buffer(masterKey, "base64");

var text = (verb || "").toLowerCase() + "\n" +
(resourceType || "").toLowerCase() + "\n" +
(resourceId || "") + "\n" +
(headers["x-ms-date"] || "").toLowerCase() + "\n" +
(headers["date"] || "").toLowerCase() + "\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;
},

因此,如果您想列出数据库,因为没有资源 ID,您将需要为 link 变量使用空字符串。同样,如果您想列出数据库中的集合,则链接实际上应该是数据库的 ID(例如 dbs/00001 而不是 dbs/00001/colls)。

关于azure - DocumentDB REST API - 授权 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com