Ok so the problem was not related to managed identity.
Clearly that was set up correctly. The issue was to do with the parameters.
The correct code is as follows:
`DROP DATABASE SCOPED CREDENTIAL [https://<serviceBusName>.servicebus.windows.net/<queueName>]`
OPEN MASTER KEY DECRYPTION BY PASSWORD='<thePassword>'
CREATE DATABASE SCOPED CREDENTIAL [https://<serviceBusName>.servicebus.windows.net/<queueName>] WITH IDENTITY = 'Managed Identity', SECRET = '{"resourceid":"https://servicebus.azure.net"}';
DECLARE @response NVARCHAR(MAX);
DECLARE @url NVARCHAR(4000) = N'https://<serviceBusName>.servicebus.windows.net/<queueName>/messages';
DECLARE @Guid NVARCHAR(40) = NEWID();
DECLARE @payload NVARCHAR(MAX) = '{ "message": "Hello, world! ' + @Guid + '" }';
EXEC sp_invoke_external_rest_endpoint
@method = N'POST',
@url = @url,
@credential = [<serviceBusName>.servicebus.windows.net/<queueName>],
@payload = @payload,
@response = @response OUTPUT;
PRINT @Guid + ' ' + @response;
The Guid is added so that when testing the Guid of the message can be correlated with the message in the Service Bus Explorer.
80F40412-5445-4269-8899-D01CFA8863B5
{
"response":{
"status":{
"http":{
"code":201,
"description":"Created"
}
},
"headers":{
"Date":"Fri, 19 Jul 2024 08:38:04 GMT",
"Transfer-Encoding":"chunked",
"Content-Type":"application\/xml; charset=utf-8",
"Server":"Microsoft-HTTPAPI\/2.0",
"Strict-Transport-Security":"max-age=31536000"
}
}
}