简体   繁体   English

ServiceBus Node.Js SDK 在创建队列时抛出错误

[英]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.我正在使用 npm 中的"azure-sb": "^0.11.0" 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.尝试启动时出现问题:错误:401 - InvalidAudience:授权标头包含具有错误受众的令牌。 TrackingId: ******, SystemTracker:*****.servicebus.windows.net:Endpoint=sb:/******.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=******* 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.当 javascript 中的设备名称与您生成的 SAS 密钥中的发布者名称不同步时,通常会出现问题。

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.当代码中的 deviceName 变量(在 POST URL 中使用)与 SAS 键中的发布者名称匹配时,无论是否注释掉附加标头,事情都可以正常工作。

When I change the device name to NOT match the Publisher in the SAS key, it immediately throws the Invalid Authorization Token Audience error.当我将设备名称更改为与 SAS 密钥中的发布者不匹配时,它会立即引发无效授权令牌受众错误。

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.只要 deviceName 与 SAS 中的 Publishers 标记相匹配,这件事一整天都在正常工作,并且注释掉了附加标头。

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.据我所知,如果你README.md阅读了Azure/azure-sdk-for-node README.md文档,你会发现 Azure 服务总线有如下三个节点包。

  1. azure-sb in Azure service modules Azure 服务模块中的azure-sb
  2. azure-arm-sb in Azure Resource Management (ARM) Azure 资源管理 (ARM) 中的azure-arm-sb
  3. azure-asm-sb in Azure Service Management (ASM) Azure 服务管理 (ASM) 中的azure-asm-sb

Actually, they required you to use different authentication ways to use different features, please see the doc Authentication.md carefully.实际上,他们要求您使用不同的身份验证方式来使用不同的功能,请仔细查看文档Authentication.md

So for azure-sb , it seems to be used for accessing Azure ServiceBus service like the npm package description said as below.所以对于azure-sb ,它似乎用于访问 Azure ServiceBus 服务,就像下面所说的 npm 包描述。

Microsoft Azure SDK for Node.js - Gallery适用于 Node.js 的 Microsoft Azure SDK - 库

This project provides a Node.js package for accessing the Azure ServiceBus service.该项目提供了一个用于访问 Azure ServiceBus 服务的 Node.js 包。

To create a Queue for a servicebus instance, please refer to the related REST API Create Queue which is a Resource Management API.要为服务总线实例创建队列,请参阅相关的 REST API Create Queue ,这是一个资源管理 API。 So I recommended the correct node package you needed is azure-arm-sb with Service Principal Authentication .所以我推荐你需要的正确节点包是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 .请参阅azure-arm-sb package更多详细信息。 Hope it helps.希望能帮助到你。

azure-sb library is the older service-bus SDK. azure-sb库是较旧的服务总线 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.虽然我认为它应该可以工作,但我建议使用最近发布的@azure/service-bus的最新版本 7.0.0。

Version 7 offers ServiceBusAdministrationClient that lets you manage the service bus entities, supporting the authentication with connection strings and Azure Active Directory credentials.版本 7 提供ServiceBusAdministrationClient ,可让您管理服务总线实体,支持使用连接字符串和 Azure Active Directory 凭据进行身份验证。

For the problem in this question, here is an example using ServiceBusAdministrationClient showing queue creation - administrationClient.ts对于这个问题中的问题,这里是一个使用ServiceBusAdministrationClient显示队列创建的示例 - 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.我知道这是一个迟到的回复,但以防万一有人遇到节点的旧服务总线 SDK 的问题并登陆这里,请参阅下面的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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