在Azure Cosmos DB中使用REST进行CRUD操作 [英] Using REST for CRUD operations in Azure Cosmos DB
问题描述
我正在尝试使用REST在Azure Cosmos DB上进行CRUD操作.根据链接- https://docs.microsoft.com/zh-我们/rest/api/documentdb/create-a-document 我已经创建了有效负载,并尝试在Opera浏览器中使用Restman对其进行测试.以下是我的有效负载详细信息-
I am trying do CRUD operations on Azure Cosmos DB using REST. As per the link- https://docs.microsoft.com/en-us/rest/api/documentdb/create-a-document I have created my payload and trying to test it using Restman in Opera browser. Below is my payload details-
标题
Authorization ***************************
Content-Type application/query+json
x-ms-date Tue, 05 Dec 2017 16:49:31 GMT
x-ms-session-token Session
x-ms-version 2017-02-22
身体
id sg4c828f-31f8-4db4-8e7c-e8bdff222dsg
value { "id": "AndersenFamily", "LastName": "Andersen", "Parents": [ { "FamilyName": null, "FirstName": "Thomas" }, { "FamilyName": null, "FirstName": "Mary Kay" } ], "Children": [ { "FamilyName": null, "FirstName": "Henriette Thaulow", "Gender": "female", "Grade": 5, "Pets": [ { "GivenName": "Fluffy" } ] } ], "Address": { "State": "WA", "County": "King", "City": "Seattle" }, "IsRegistered": true }
使用以下代码(根据上述链接中的示例),使用C#生成了放置在请求标头中的auth-token-
The auth-token which is put in the request header has been generated in C# using below code (as per the sample in the link mentioned above)-
string GenerateAuthToken(string verb, string resourceType, string resourceId, string date, string key, string keyType, string tokenVersion)
{
var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };
verb = verb ?? "";
resourceType = resourceType ?? "";
resourceId = resourceId ?? "";
string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\n{1}\n{2}\n{3}\n{4}\n",
verb.ToLowerInvariant(),
resourceType.ToLowerInvariant(),
resourceId,
date.ToLowerInvariant(),
""
);
byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));
string signature = Convert.ToBase64String(hashPayLoad);
return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
keyType,
tokenVersion,
signature));
}
这是传递参数的方式-
GenerateAuthToken("GET", "dbs", "dbs/ToDoList", "Tue, 05 Dec 2017 16:49:31 GMT", PARENT_KEY, "master", "1.0");
因此,当我向URL发出POST请求时-
So when I make the POST request to the URL-
https://<account_name>.documents.azure.com:<port>/dbs/DCEAAA==/colls/DCEAAIcEVAA=/docs
我没有得到回应-
{ "code":未经授权", "message":输入的授权令牌不能满足请求.请检查是否根据协议构建了预期的有效负载, 并检查正在使用的密钥.服务器使用以下有效负载来 签名:'post \ ndocs \ ndceaaicevaa = \ ntue,2017年12月5日16:49:31 gmt \ n \ n'\ r \ nActivityId:7565996c-d008-438d-a1e9-744d4871948a, Microsoft.Azure.Documents.Common/1.19.121.4}
{ "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\ndceaaicevaa=\ntue, 05 dec 2017 16:49:31 gmt\n\n'\r\nActivityId: 7565996c-d008-438d-a1e9-744d4871948a, Microsoft.Azure.Documents.Common/1.19.121.4" }
我不知道这里到底出了什么问题.如果有人有任何想法,请告诉我.如有任何澄清,请通知我.
I am clueless what exactly is going wrong here. Please let me know if someone have any ideas. For any clarifications please let me know.
编辑:在下方添加Restman屏幕截图-
Adding the Restman screenshot below-
推荐答案
我跟进了您提到的Document Create Document API,并进行了演示以使用其余API创建documentdb文档.您可以参考它.对于其他操作,您可以按照以下代码并构造哈希标记.
I follow up you mentioned Document Create Document API, and do a demo to create documentdb document with rest API. You could refer to it. For other operates you could follow the following code and construct the hashed token.
根据通用Azure Cosmos DB REST请求标头,如果要创建文档,则需要按以下方式准备标头
According to the Common Azure Cosmos DB REST request headers, if we want to create a document we need to prepare the Header as following
Authorization,x-ms-date,Content-Type,x-ms-version
我们可以从此文档中获取xm版本. .最新版本为 2017-02-22 .
We could get the x-m-version from this document. The latest version is 2017-02-22.
我们可以从文档.从文档中我们可以知道resourceType可能是"dbs", "colls", "docs".
,我们需要创建文档,因此 resourceType = docs
We could get the demo code you mentioned from this document,about how to constructing the hashed token signature for a master token please refer to this document. From the document we could know that resourceType could be "dbs", "colls", "docs".
We need to create a document, so resourceType = docs
var databaseId = "databaseName";
var collectionId = "collectionName";
var datetime = DateTime.UtcNow.ToString("R");
var verb = "post";
var resourceType = "docs"; //
var resourceId = $"dbs/{databaseId}/colls/{collectionId}";
var mastKey = "mastkey value";
var keyType = "master";
var tokenVersion = "1.0";
var authToken = GenerateAuthToken(verb, resourceType, resourceId, datetime, mastKey, keyType, tokenVersion);
我们还需要从上述代码变量 datetime
We also need to get the x-ms-date value from the above mentioned code varible datetime
Post https://{documentDBAccount}.documents.azure.com:443/dbs/{databaseName}/colls/{collectionId}/docs
更新:
请使用以下数据作为json正文
Please use the following data as json body
{
"id": "sg4c828f-31f8-4db4-8e7c-e8bdff222dsg",
"value": {
"id": "AndersenFamily",
"LastName": "Andersen",
"Parents": [
{
"FamilyName": null,
"FirstName": "Thomas"
},
{
"FamilyName": null,
"FirstName": "Mary Kay"
}
],
"Children": [
{
"FamilyName": null,
"FirstName": "Henriette Thaulow",
"Gender": "female",
"Grade": 5,
"Pets": [
{
"GivenName": "Fluffy"
}
]
}
],
"Address": {
"State": "WA",
"County": "King",
"City": "Seattle"
},
"IsRegistered": true
}
}
这篇关于在Azure Cosmos DB中使用REST进行CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!