我正在嘗試使用 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 關注
- 223 瀏覽
添加回答
舉報
0/150
提交
取消