401(未授权)| Cosmos DB 休息 API |来自.net 的休息

401 (Unauthorized) | Cosmos DB rest API | rest-from-.net

我正在为 Cosmos DB rest-from-.net 试用 Table API.

设置endpointmasterKeydatabaseIdcollectionId。我正在使用内部资源 ID。将 x-ms-version 设为 2017-07-29.

line 84 之前添加了以下内容。

client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("ContentType", "application/json");

现在,它看起来像下面这样:

client.DefaultRequestHeaders.Add("x-ms-date", utc_date);
client.DefaultRequestHeaders.Add("x-ms-version", "2017-07-29");

//LIST all databases
verb = "GET";
resourceType = "dbs";
resourceId = string.Empty;
resourceLink = string.Format("dbs");

authHeader = GenerateMasterKeyAuthorizationSignature(verb, resourceId, resourceType, masterKey, "master", "1.0");

client.DefaultRequestHeaders.Remove("authorization");
client.DefaultRequestHeaders.Add("authorization", authHeader);
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("ContentType", "application/json");

response = client.GetStringAsync(new Uri(baseUri, resourceLink)).Result;

GetStringAsync 呼叫中,我收到 401。

HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).

Either the Authorization or x-ms-date header is not set. 401 is also returned when the Authorization header is set to an invalid authorization token.

为什么授权失败?

虽然Querying Azure Cosmos DB resources using the REST API describes usage of REST for Cosmos DB, such use of REST is possible only for SQL API not for Gremlin, Cassandra or Table API. Unfortunately, the document doesn't mention that. Hence, the authorization was failing. Instead refer Table Service REST API.