我正在尝试使用REST对Azure Cosmos数据库执行CRUD操作。根据链接 - https://docs.microsoft.com/en-us/rest/api/documentdb/create-a-document 我创建了我的有效负载并试图在Opera浏览器中使用Restman进行测试。以下是我的有效载荷详情 -
接头
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(根据上述链接中的示例) -
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请求时
https://<account_name>.documents.azure.com:<port>/dbs/DCEAAA==/colls/DCEAAIcEVAA=/docs
我得到以下回复 -
{ &#34;代码&#34;:&#34;未经授权&#34;, &#34; message&#34;:&#34;输入授权令牌无法提供请求。请检查是否按照协议构建了预期的有效负载, 并检查正在使用的密钥。服务器使用以下有效负载 sign:&#39; post \ ndocs \ ndceaaicevaa = \ ntue,05 dec 2017 16:49:31 gmt \ n \ n&#39; \ r \ nActivityId:7565996c-d008-438d-a1e9-744d4871948a, Microsoft.Azure.Documents.Common / 1.19.121.4&#34; }
我不知道这里到底出了什么问题。如果有人有任何想法,请告诉我。如有任何澄清,请告诉我。
答案 0 :(得分:2)
我跟进你提到的文档创建文档API,并做一个演示来创建带有rest API的documentdb文档。你可以参考它。对于其他操作,您可以按照以下代码构造哈希标记。
根据Common Azure Cosmos DB REST request headers,如果我们想创建一个文档,我们需要准备Header如下
Authorization,x-ms-date,Content-Type,x-ms-version
我们可以从document获得x-m版本。最新版本为 2017-02-22 。
我们可以从这个document获取您提到的演示代码,关于如何为主令牌构建散列令牌签名,请参阅此document。从文档我们可以知道resourceType可能是"dbs", "colls", "docs".
我们需要创建文档,所以 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);
我们还需要从上面提到的代码日期时间
获取 x-ms-date 值Post https://{documentDBAccount}.documents.azure.com:443/dbs/{databaseName}/colls/{collectionId}/docs
<强>更新强>
请使用以下数据作为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
}
}