我试图将documentdb支持添加到用dotnetcore编写的现有应用程序中,因此我无法使用SDK,但我认为我只是使用REST API。很容易,样品很容易复制。我可以很好地完成REST API示例中的所有列表/查询。
尝试创建文档时出现问题。我不断面对401,似乎无法绕过它。我使用主键 - 而不是只读键。我在这里阅读并重新阅读了API文档:https://msdn.microsoft.com/en-us/library/azure/mt489088.aspx,但无法使其发挥作用。
可能使用我的身份验证密钥,但我使用了示例GenerateMasterKeyAuthorizationSignature(string verb, string resourceId, string resourceType, string key, string keyType, string tokenVersion)
中的方法,这对查询非常有用。我使用基于ID的resourceId,并认为我可以重用之前的代码:
resourceLink = string.Format("dbs/{0}/colls/{1}/docs", databaseId, collectionId);
resourceId = (idBased) ? string.Format("dbs/{0}/colls/{1}/docs", databaseId, collectionId) : collectionId.ToLowerInvariant();`
时间戳似乎是正确的,因为查询有效,尽管我已经看到其他地方所说的问题。我正在使用Ryan的PostWithNoCharSetAsync
我曾尝试使用client.DefaultRequestHeaders.Add("x-ms-documentdb-is-upsert", "true");
分区有什么问题吗?我应该指定吗?
API文档包含Cookie和no-cache等标头。那些不是有所作为吗?
如果JSON文档遇到字符串文字标记,这是否重要 - 如下所示:" {\r\n \"id\": 4441,\r\n \"Name\": \"Artesia Spa - Grand Hotel\",\r\n }
以下是来自fiddler的POST。
请求
POST https://etest.documents.azure.com/dbs/etest/colls/unittest/docs HTTP/1.1
x-ms-date: Tue, 08 Nov 2016 20:34:40 GMT
x-ms-version: 2015-12-16
authorization: type%3dmaster%26ver%3d1.0%26sig%3dU8BmnPhUMWyoVqNdbI0hy1Kc%2b1Yge79dCBMz8f2v9pE%3d
x-ms-documentdb-is-upsert: true
Content-Type: application/query+json
Host: etest.documents.azure.com
Content-Length: 48
Expect: 100-continue
{"id": 4441,"Name": "Artesia Spa - Grand Hotel"}
RESPONSE
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
x-ms-activity-id: d83ae44f-3dc8-47a6-b310-cdf8ca87c597
Strict-Transport-Security: max-age=31536000
x-ms-gatewayversion: version=1.10.39.1
Date: Tue, 08 Nov 2016 20:36:11 GMT
Content-Length: 358
{"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/etest/colls/unittest\ntue, 08 nov 2016 20:34:40 gmt\n\n'\r\nActivityId: d83ae44f-3dc8-47a6-b310-cdf8ca87c597"}
答案 0 :(得分:1)
现在有一个官方的.Net Core SDK,您可以像以前的SDK from Nuget一样获取和使用。
如果您需要完整样本,可以查看我发布的GitHub repo,其中涵盖了最常见的情况。
答案 1 :(得分:0)
由于我只花如2天尝试弄清楚这一点,这里有兴趣的人一个实际的答案。现在,DocumentDB现在是CosmosDB。
创建DB 强>
var verb = "POST";
var resourceId = "";
var resourceType = "dbs";
var resourceLink = "dbs";
var body = new { id = "<databaseId>" };
创建集合强>
var verb = "POST";
var resourceId = "dbs/<databaseId>";
var resourceType = "colls";
var resourceLink = "dbs/<databaseId>/colls";
var body = new { id = "<collectionId>" };
创建文档强>
var verb = "POST";
var resourceId = "dbs/<databaseId>/colls/<collectionId>";
var resourceType = "docs";
var resourceLink = "dbs/<databaseId>/colls/<collectionId>/docs";
var body = new { id = "<documentId>" };
创建用户强>
var verb = "POST";
var resourceId = "dbs/<databaseId>";
var resourceType = "users";
var resourceLink = "dbs/<databaseId>/users";
var body = new { id = "<userId>" };
**基本上RESOURCEID相同resourceLink但没有最后的选项(其不明显我)
设置请求类似(see the example program from msft for the rest)
var client = new System.Net.Http.HttpClient();
string response = string.Empty;
string authHeader = string.Empty;
authHeader = GenerateMasterKeyAuthorizationSignature(verb, resourceId, resourceType, key, keyType, tokenVersion);
client.DefaultRequestHeaders.Add("x-ms-date", utc_date);
client.DefaultRequestHeaders.Add("x-ms-version", "2017-02-22");
client.DefaultRequestHeaders.Remove("authorization");
client.DefaultRequestHeaders.Add("authorization", authHeader);
同时请注意,从MSFT样本程序包括用于查询一个NoCharSetJsonMediaTypeFormatter。对于POST,需要对此进行更新,因为标头必须为“ application / json”而不是“ application / query + json”