【发布时间】:2021-02-24 08:47:35
【问题描述】:
我正在尝试使用 Azure 服务总线 api 从某些队列中清除消息。
根据文档 (https://docs.microsoft.com/en-us/rest/api/storageservices/clear-messages),我将使用以下请求来执行此操作:
https://myaccount.queue.core.windows.net/myqueue/messages
这看起来很简单,但问题是我无法让这个请求正确授权。 (结果 = 401 未经授权)
显然,签名过程相当复杂,并在此处进行了描述 (https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key)
我做了很多尝试,比如下面的代码:
var httpClient = new HttpClient();
using (HMACSHA256 hmac = new HMACSHA256())
{
var StringToSign = $"GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:{DateTime.UtcNow.ToString("o")}\nx-ms-version:2015-02-21\n\n\n\n";
var stringToSignHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(StringToSign));
var base64StringToSign = Convert.ToBase64String(stringToSignHash);
var signature = $"{base64StringToSign}, {MySecretKey}";
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Delete, "https://myaccount.servicebus.windows.net/myqueue/messages"))
{
//requestMessage.Headers.Add("x-ms-date", DateTime.UtcNow.ToString());
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("SharedKey", signature);
var result = httpClient.SendAsync(requestMessage).GetAwaiter().GetResult();
}
}
对于 MySecretKey,我使用了我的共享访问策略密钥。我尝试按原样使用,并将base64解码为ascii。
有人在这方面取得了更大的成功吗? 有没有更简单的方式访问api?
谢谢。
【问题讨论】:
标签: azure azureservicebus azure-authentication