简体   繁体   中英

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

I am trying out rest-from-.net for Cosmos DB Table API .

Set the endpoint , masterKey , databaseId , collectionId . I am using internal resource ids. Set x-ms-version as 2017-07-29 .

Added the following before line 84 .

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

Now, it looks like below:

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;

On the GetStringAsync call, I get 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.

Why is the authorization failing?

New

You should use offical sample to connect cosmosdb table.

azure-cosmos-table-dotnet-core-getting-started

在此处输入图像描述

Privious

You should add bear token like below code.

client.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue("Bearer", "Your Oauth token");

Though 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM