John Brunner
John Brunner

Reputation: 2872

DocumentDB: Can't access documents in my collection

I'm using DocumentDB, and want to access via REST-API a specific document from one of my collections with a submitted query.

I want to test the call with Postman. I configured the headers and the authentication-token, and everything works as expected (for example I'm able to get a list of all of my collections), but I can't read the documents.

I'm calling the following URI:

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

Authorization Token is generated with the following parameters:

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

Headers are:

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

Body is (x-www-form-urlencoded):

query: SELECT * FROM offer
parameters: []

But the response from Postman is always:

{
  "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"
}

What am I doing wrong?

Upvotes: 2

Views: 2093

Answers (3)

ArTrejo-MSFT
ArTrejo-MSFT

Reputation: 171

I tried to reproduce your scenario, and I was able to successfully query documents using Postman.

To help you troubleshoot, here are the parameters I used to compute the token (including a dummy key so you can try as well):

  • Auth Token Parameters:

    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"
    
  • The resulting token is:

    type%3dmaster%26ver%3d1.0%26sig%3dC373dEIadokxR4z%2bSSpmbpVYw4e7mk9Vhae%2f5nLaD%2bU%3d
    
  • Request verb and URI

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

    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
    
  • Body:

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

Notice I used a different Content-Type from the one you are using. I don't think this is what's causing your issue, since you would get a Bad Request response if that were the problem.

To summarize, I believe the issue is within your token generator, since I used the same parameters as you to generate the token, and the same URI for the POST request.

I am posting a dummy masterkey, date, and resulting token so you can use it to test your token generator. Hope this help to narrow down the source of your issue.

Upvotes: 1

Gary Liu
Gary Liu

Reputation: 13918

It seems that you have misused the Rest API. If you are attempting to Querying Offers, the POST uri should be like with https://{databaseaccount}.documents.azure.com/offers.

And if you are attempting to Query Documents (which may actually be the one you want to achieve). The POST uri should be https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs

Please try to follow https://msdn.microsoft.com/en-us/library/azure/mt670897.aspx to impelemnt the Rest API to query documents.

update

And try to generate Authorization token with:

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

I used the code snippet in this answer of DocumentDB REST API with Postman outputs always "Unauthorized" error,

getAuthorizationTokenUsingMasterKey("POST", "docs", "dbs/testdb/colls/offer", headers, "{key}"));

to generate the authorization token. And worked fine on PostMan: enter image description here

Upvotes: 1

Russell Young
Russell Young

Reputation: 2043

I found this a bit frustrating to get working too, so I created a blog post that takes you through it - using powershell to build the calls, but the process and format of the strings is the important piece. I had issues with the date formatting, and what strings to use to identify the resources you need to query.

Take a look at Managing DocumentDB with powershell - I found the error messages were fairly important, check the inputs you create vs the detail in the output error message.

Upvotes: 0

Related Questions