简体   繁体   中英

ServiceBus Node.Js SDK throwing error upon queue creation

For obvious reasons parts of the connection string / domain names are obfuscated with stars.

I'm using "azure-sb": "^0.11.0" from npm. Below is the code snippet where I'm attempting to create the queue.

/* Connection string taken from Azure Portal shared access policies */
let serviceBusService = azure.createServiceBusService(SERVICEBUS_CONNECTION_STRING);

serviceBusService.createQueueIfNotExists(SERVICE_BUS_QUEUE_NAME, function(error){
    if(!error){
        console.log(`Looks like we'll be up and running.`);
    }
    /* This statement gets executed. */
    else {
        console.error(`Something went wrong when trying to boot up: ${error}`);
    }
});

Something went wrong when trying to boot up: Error: 401 - InvalidAudience: The authorization header contains a token with a wrong audience. TrackingId: ******, SystemTracker:*****.servicebus.windows.net:Endpoint=sb:/******.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=*******

I haven't changed any settings, using the default connection string (setting no audience in any place as far as I can see) and it's failing. Any ideas?

Issue appears usually when the device name in the javascript out of sync with the publisher name in the SAS key that you generated.

Eg

在此处输入图片说明

When the deviceName variable in the code (which is used in the POST URL) matches the Publisher name in the SAS key, the thing works fine, whether or not the additional headers are commented out or not.

When I change the device name to NOT match the Publisher in the SAS key, it immediately throws the Invalid Authorization Token Audience error.

This thing has been working fine all day with the additional headers commented out, as long as the deviceName matches the Publishers token in the SAS.

try to validate and see if it helps.

As I known, if you have read the doc README.md of Azure/azure-sdk-for-node carefully, you will find there are three node packages for Azure Service Bus as below.

  1. azure-sb in Azure service modules
  2. azure-arm-sb in Azure Resource Management (ARM)
  3. azure-asm-sb in Azure Service Management (ASM)

Actually, they required you to use different authentication ways to use different features, please see the doc Authentication.md carefully.

So for azure-sb , it seems to be used for accessing Azure ServiceBus service like the npm package description said as below.

Microsoft Azure SDK for Node.js - Gallery

This project provides a Node.js package for accessing the Azure ServiceBus service.

To create a Queue for a servicebus instance, please refer to the related REST API Create Queue which is a Resource Management API. So I recommended the correct node package you needed is azure-arm-sb with Service Principal Authentication .

The sample code without interaction is like as below.

const Azure = require('azure');
const MsRest = require('ms-rest-azure');
const ServiceBusManagementClient = require("azure-arm-sb");

MsRest.loginWithServicePrincipalSecret(
  'clientId or appId',
  'secret or password',
  'domain or tenantId',
  (err, credentials) => {
    if (err) throw err

    const client = new ServiceBusManagementClient(credentials, 'subscriptionId');

    // ..use the client instance to manage service resources.
    client.Queues.createOrUpdate(resourceGroupName, namespaceName, queueName, parameters: sbQueue, function(sbq) {
    })

  }
);

Please see more details for azure-arm-sb package . Hope it helps.

azure-sb library is the older service-bus SDK. Though I believe it should be working, I'd suggest using the latest version 7.0.0 of @azure/service-bus that has been published recently.

Version 7 offers ServiceBusAdministrationClient that lets you manage the service bus entities, supporting the authentication with connection strings and Azure Active Directory credentials.

For the problem in this question, here is an example using ServiceBusAdministrationClient showing queue creation - administrationClient.ts

I know this is a late reply, but just in case someone is running into issues with the older service-bus SDK for node and landed here, refer to the links below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM