I created service bus with the queue, but when I try to test via POSTMAN I am getting 401

Balaji Ravikumar 0 Reputation points
2024-02-28T04:37:12.1+00:00

Hi I am using POST method to send message to service bus queue using Azure Active Directory (Azure AD) JSON Web Token (JWT), I am able to generate the successful authenticate token but when I try to hit the post api in Postman using that token getting 401 error. http{s}://{serviceNamespace}.servicebus.windows.net/{queuePath|topicPath}/messages|HTTP/1.1 Please do the needful. Thanks Balaji

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
613 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,141 Reputation points
    2024-02-28T05:31:19.7833333+00:00

    @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. enter image description here

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.