@Balaji Ravikumar I have reviewed and tested the same at my end using powershell script and I don't see any issue at my end.
$tenantId = "yourtenantid"
$clientId = "yourclientid"
$clientSecret = "yoruSecret"
$serviceBusNamespace = "namespacename"
$messageBody = "test"
$resource = "https://servicebus.azure.net/"
$queueName = "test"
$body = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
resource = $resource
}
$tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$token = Invoke-RestMethod -Method Post -Uri $tokenEndpoint -Body $body
$accessToken = $token.access_token
$headers = @{
"Authorization" = "Bearer $accessToken"
"Content-Type" = "application/json"
}
$body = @{
"Body" = $messageBody
} | ConvertTo-Json
$url = "https://$serviceBusNamespace.servicebus.windows.net/$queueName/messages?api-version=2017-04"
Invoke-RestMethod -Method Post -Uri $url -Headers $headers -Body $body
As you are getting 401 error so I will suggest to validate this step whether you have given the right permission to your AD application. Please validate application display name matches with the role assignment that you have provided on your same service bus resource.
Validate whether you are passing the application ID, tenant ID correctly.