Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# documentDB Rest API-创建文档_C#_Json_Rest_Azure Cosmosdb_Http Status Code 401 - Fatal编程技术网

C# documentDB Rest API-创建文档

C# documentDB Rest API-创建文档,c#,json,rest,azure-cosmosdb,http-status-code-401,C#,Json,Rest,Azure Cosmosdb,Http Status Code 401,我正在尝试将documentdb支持添加到用dotnetcore编写的现有应用程序中,因此我不能使用SDK,但我认为我应该使用RESTAPI。很简单,样品很容易复制。我可以很好地完成RESTAPI示例中的所有列表/查询 在尝试创建文档时会出现问题。我一直面临着401计划,似乎无法摆脱它。我使用的是主键,而不是只读键。我已经在这里反复阅读了API文档:但不能完全让它工作 它可能与我的身份验证密钥有关,但我使用的是示例GenerateMasterKeyAuthorizationSignature(字

我正在尝试将documentdb支持添加到用dotnetcore编写的现有应用程序中,因此我不能使用SDK,但我认为我应该使用RESTAPI。很简单,样品很容易复制。我可以很好地完成RESTAPI示例中的所有列表/查询

在尝试创建文档时会出现问题。我一直面临着401计划,似乎无法摆脱它。我使用的是主键,而不是只读键。我已经在这里反复阅读了API文档:但不能完全让它工作

它可能与我的身份验证密钥有关,但我使用的是示例
GenerateMasterKeyAuthorizationSignature(字符串动词、字符串资源ID、字符串资源类型、字符串密钥、字符串密钥类型、字符串令牌版本)中的方法
,这对查询非常有效。我正在使用基于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之类的头,没有缓存。这些都没什么区别,是吗

如果JSON文档遇到字符串文字标记(如:
“{\r\n\“id\”:4441、\r\n\“Name\”:“Artesia Spa-Grand Hotel\”,\r\n},这有关系吗

下面是菲德勒的帖子

请求

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"}
响应

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

现在您可以获得并使用官方的.Net核心SDK,就像以前的SDK一样


如果您需要完整的样本,您可以查看我发布的一份报告,其中涵盖了最常见的场景。

因为我花了大约2天的时间试图弄清楚这一点,这里有一个实际的答案供感兴趣的人参考。DocumentDB现在是CosmosDB

创建数据库

var verb = "POST";
var resourceId = "";
var resourceType = "dbs";
var resourceLink = "dbs"; 
var body = new { id = "<databaseId>" };

还请注意,MSFT的示例程序包括一个用于查询的NoCharSetJsonMediaTypeFormatter。这需要为帖子更新,因为标题需要为“application/json”,而不是“application/query+json”“

您好,您介意发布GenerateMasterKeyAuthorizationSignature的代码吗?使用代码进行故障排除会更容易。请注意,如果使用资源id构造令牌,则resourceid必须保持与资源的大小写匹配,例如。我收集。这里有一个使用RESTAPI构造身份验证令牌字符串的参考:AAlso,您能看一下并将您的实现与之进行比较吗?
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>" };
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);