DocumentDB:无法访问我的集合中的文档

时间:2016-09-30 13:57:57

标签: rest azure postman azure-cosmosdb

我正在使用DocumentDB,并希望通过REST-API从我的document之一使用提交的查询访问特定的collections

我想和Postman一起测试这个电话。我配置了标头和身份验证令牌,一切都按预期工作(例如,我能够获得所有collections的列表),但我无法阅读documents

我正在调用以下URI:

POST https://xxx.azure.com/dbs/testDB/colls/offer/docs

使用以下参数生成授权令牌:

verb: "POST"
resourceType: "docs"
resourceID: "dbs/testDB/colls/offer"
x-ms-date: ...
masterkey: "P8riQ...aMORA=="

标题是:

x-ms-documentdb-isquery: True
x-ms-date: ...
authorization: generated token
x-ms-version: 2015-08-06
Content-Type: application/x-www-form-urlencoded

正文是(x-www-form-urlencoded):

query: SELECT * FROM offer
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/testDB/colls/offer\nfri, 30 sep 2016 13:47:32 gmt\n\n'\r\nActivityId: a76408d...e08cef"
}

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您似乎滥用了Rest API。如果您尝试Querying Offers,则POST uri应该与https://{databaseaccount}.documents.azure.com/offers一样。

如果您正在尝试Query Documents(实际上可能是您想要实现的那个)。 POST uri应为https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs

请尝试关注https://msdn.microsoft.com/en-us/library/azure/mt670897.aspx以阻止Rest API查询文档。

更新

尝试使用以下命令生成授权令牌:

verb: "POST"
resourceType: "docs"
resourceID: "dbs/{db-id}/colls/{coll-id}"
x-ms-date: ...
masterkey: "P8riQ...aMORA=="

我在DocumentDB REST API with Postman outputs always "Unauthorized" error

的答案中使用了代码段
getAuthorizationTokenUsingMasterKey("POST", "docs", "dbs/testdb/colls/offer", headers, "{key}"));

生成授权令牌。在PostMan上工作得很好: enter image description here

答案 1 :(得分:1)

我尝试重现您的方案,并且我能够使用Postman成功查询文档。

为了帮助您排除故障,以下是我用来计算令牌的参数(包括虚拟钥匙以便您也可以尝试):

  • 验证令牌参数:

    verb: "POST"
    resourceId: "dbs/testDB/colls/offer"
    resourceType: "docs"
    key: "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmn=="
    keyType: "master"
    tokenVersion: "1.0
    date: "Thu, 06 Oct 2016 04:31:33 GMT"
    
  • 生成的令牌是:

    type%3dmaster%26ver%3d1.0%26sig%3dC373dEIadokxR4z%2bSSpmbpVYw4e7mk9Vhae%2f5nLaD%2bU%3d
    
  • 请求动词和URI

    POST https://xxx.documents.azure.com/dbs/testDB/colls/offer/docs
    
  • 接头:

    authorization: type%3dmaster%26ver%3d1.0%26sig%3dC373dEIadokxR4z%2bSSpmbpVYw4e7mk9Vhae%2f5nLaD%2bU%3d
    x-ms-date: Thu, 06 Oct 2016 04:31:33 GMT
    x-ms-version: 2015-08-06
    x-ms-documentdb-isquery: true
    Content-Type: application/query+json
    
  • 体:

    {"query":"SELECT * FROM c"}
    

注意我使用的是与您正在使用的内容类型不同的内容类型。我不认为这是导致您出现问题的原因,因为如果出现问题,您会收到错误请求回复。

总而言之,我认为问题出在您的令牌生成器中,因为我使用了与您相同的参数来生成令牌,以及POST请求的相同URI。

我发布了一个虚拟的主密钥,日期和结果令牌,因此您可以使用它来测试令牌生成器。希望这有助于缩小问题的根源。

答案 2 :(得分:0)

我发现这也让我感到有点沮丧,因此我创建了一篇博文,引导您完成它 - 使用PowerShell来构建调用,但字符串的过程和格式是重要的一部分。我遇到了日期格式问题,以及用于识别需要查询的资源的字符串。

看看Managing DocumentDB with powershell - 我发现错误消息非常重要,请检查您创建的输入与输出错误消息中的详细信息。