我正在嘗試使用 Table API 訪問 Azure Cosmos DB。挑戰(zhàn)是,盡管創(chuàng)建了 SharedKeyLite,服務(wù)器仍然返回 Unauthorized - 似乎 SharedKeyLite 不受支持,或者我生成的簽名或標(biāo)頭錯誤。這是代碼 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; } } }這些是我添加的標(biāo)題,ConstructHeaders 方法的擴(kuò)展。請參閱此鏈接以獲取請求參數(shù)
1 回答

慕絲7291255
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊
只需復(fù)制您的代碼,它就可以在我身邊工作。如果您還沒有修改您的代碼,請確保您的storageAccountName
和storageAccountKey
是正確的。
順便說一句,在方法中GenerateSharedKeyLite
不需要canonicalizedResourceString
為實(shí)體操作添加查詢參數(shù)。comp
如果要操作表或服務(wù)的組件信息,則只需添加。請參閱構(gòu)建規(guī)范化資源字符串。
查詢字符串應(yīng)包括問號和
comp
參數(shù)(例如,?comp=metadata
)。查詢字符串中不應(yīng)包含其他參數(shù)。
- 1 回答
- 0 關(guān)注
- 221 瀏覽
添加回答
舉報(bào)
0/150
提交
取消