bluedot
bluedot

Reputation: 782

Authorization failed using Azure Service Bus API to manage queues

I'm trying to use the Azure Service Bus api to purge messages from certain queues.

According to the documentation (https://learn.microsoft.com/en-us/rest/api/storageservices/clear-messages) I would do this using the following request:

https://myaccount.queue.core.windows.net/myqueue/messages

This seems simple enough, but the problem is I just cannot get this request to authorize correctly. (result = 401 Unauthorized)

Apparently the signing process is rather elaborate and described here (https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key)

I had many attempts, like the following code:

                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();
                    }
                }

For MySecretKey I used my shared access policy key. I tried using as is, and also decoded base64 to ascii.

Does anyone have more success with this? Are there more simple ways to access the api?

Thanks.

Upvotes: 0

Views: 1039

Answers (2)

AHMED EL-HAROUNY
AHMED EL-HAROUNY

Reputation: 836

As an alternative you can use Service Bus Cloud Explorer which runs in the browser with logged-in user Azure account by default and have a purge functionality.

enter image description here

Keep in mind that it will take time to clear up queues or subscriptions. More messages it needs to purge; the more time it would take.

Upvotes: 0

MayankBargali
MayankBargali

Reputation: 750

You can leverage the code here in different languages to generate the SAS token.

Updated: Can you please confirm which API you are trying to execute? As per the code, you are calling the Delete method on myaccount.servicebus.windows.net (i.e. service bus resource). Delete API operation completes the processing of a locked message and deletes it from the queue or subscription. The samples/reference article that you have shared is for the delete messages from the storage queue. If your requirement is to purge all messages from the service bus queue then you need to use Recieve and Delete API. Alternative you can also use service bus explorer to purge the messages.

Upvotes: 1

Related Questions