我正在尝试使用 Table API 访问 Azure Cosmos DB。挑战是,尽管创建了 SharedKeyLite,服务器仍然返回 Unauthorized - 似乎 SharedKeyLite 不受支持,或者我生成的签名或标头错误。这是代码 static readonly string storageAccountName = "accountName"; static readonly string storageAccountKey = "xxxx"; static readonly string uri = "https://accountName.table.cosmosdb.azure.com/Contacts()"; static readonly string utc_date = DateTime.UtcNow.ToString("r"); static void Main(string[] args) { Console.WriteLine(GetResult().Result); } static async Task<string> GetResult() { // Set this to whatever payload you desire. Ours is null because // we're not passing anything in. Byte[] requestPayload = null; var requestDateString = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture); var requestUri = new Uri(uri); DateTime now = DateTime.UtcNow; //Instantiate the request message with a null payload. using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri) { Content = (requestPayload == null) ? null : new ByteArrayContent(requestPayload) }) { ConstructHeaders(httpRequestMessage.Headers, requestDateString); string authorizationHeader = GenerateSharedKeyLite(storageAccountKey, storageAccountName, uri,requestDateString); httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("SharedKeyLite", authorizationHeader); // Send the request. using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage)) { string json = await httpResponseMessage.Content.ReadAsStringAsync(); return json; } } }这些是我添加的标题,ConstructHeaders 方法的扩展。请参阅此链接以获取请求参数
1 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
只需复制您的代码,它就可以在我身边工作。如果您还没有修改您的代码,请确保您的storageAccountName
和storageAccountKey
是正确的。
顺便说一句,在方法中GenerateSharedKeyLite
不需要canonicalizedResourceString
为实体操作添加查询参数。comp
如果要操作表或服务的组件信息,则只需添加。请参阅构建规范化资源字符串。
查询字符串应包括问号和
comp
参数(例如,?comp=metadata
)。查询字符串中不应包含其他参数。
- 1 回答
- 0 关注
- 195 浏览
添加回答
举报
0/150
提交
取消