azure - 如何从前端获取来自azure服务总线主题的通知?

标签 azure azureservicebus

我们使用 azure-service-bus 进行通知服务。在azure门户中,服务总线主题已创建,并且消息在主题中可用。我正在尝试使用 REST API 服务来获取主题数据。

这是我目前拥有的代码。

import axios from 'axios';

const namespace = 'my-namespace';
const topicName = 'my-topic-name';
const subscriptionId = 'my-subscription-id';
const accesskey = 'my-access-key';

const baseurl = `https://${namespace}.servicebus.windoes.net`;
const url = `${baseurl}/${topicName}/subscriptions/${subscriptionId}`;
const headers = {
  Authorization: `SharedAccessSignature ${accesskey}`,
  'content-type': 'application/json',
}

const getDetails = async () => {
  try {
    const response = await axios.get(url, { headers });
    console.log('response', response.data);
  } catch (error) {
    console.error('error', error);
  }
}

export default getDetails;

我目前对前端所需的了解非常有限。我试图使用 @azure/service-bus .,但它给出错误 cant resolve 'os' in rhea., webpack < 5 used错误。

因此我尝试消除依赖性并使用 https服务。

我当前正在获取401 unauthorized错误。

你能指导我哪里做错了吗?

根据建议更新了代码:

import axios from 'axios';
import { SHA256 } from 'crypto-js';
   
const sbName = 'my-sb-name';
const topicName = 'my-topic-name';
const sharedAccessKey = 'my-shared-access-key';
const sharedAccessName = 'RootManagerSharedAccessKey';
const expiry = Math.floor(Date.now() / 1000) + 10000;
const baseUrl = `https://${sbName}.servicebus.windows.net`;
const url = `${baseUrl}/${topicName}`;
const toSign = `${url}\n${expiry.toString()}`;
const signature = SHA256(toSign).toString();
const encodedSignature = encodeURIComponent(signature);
const authFormat = `SharedAccessSignature sig={0}&se={1}&skn={2}$sr={3}`;
const auth = authFormat
  .replace('{0}', encodedSignature)
  .replace('{1}', expiry.toString())
  .replace('{2}', sharedAccessName)
  .replace('{3}', url);

console.log(auth);

const headers = {
  Authorization: auth,
  'Content-Type': 'application/json',
};

const getDetails = async () => {
  try {
    const response = await axios.get(url, { headers });
    console.log('Queue Details:', response.data);
    // Process the response or update your UI with the retrieved queue details
  } catch (error) {
    console.error('Error retrieving queue details:', error);
  }
};
// Call the function to get queue details
export default getDetails;

给出错误:401 unauthorized

最佳答案

I am currently getting 401 unauthorized error

这是因为您提供的授权 key 不正确。 您可以使用以下代码生成 SAS token ,然后可以在 Authorization header 中使用它来获取主题数据。

生成 SAS token 的代码

const  crypto  =  require('crypto');
const  querystring  =  require('querystring');
const  sb_name  =  'afreen-serviceBus-01';
const  topic  =  'demotopic';
const  url  =  `https://${sb_name}.servicebus.windows.net/${topic}`;
const  sas_value  =  'xxxxxxxxxxx'; // your shared access key
const  sas_name  =  'RootManageSharedAccessKey'; // your shared access rule name
const  expiry  =  Math.floor(Date.now() /  1000) +  10000;
const  to_sign  =  `${url}\n${expiry}`;
const  signature  =  crypto.createHmac('sha256', sas_value)
.update(to_sign)
.digest('base64');
const  encodedSignature  =  querystring.escape(signature);
const  auth_format  =  'SharedAccessSignature sig={0}&se={1}&skn={2}&sr={3}';
const  auth  =  auth_format.replace('{0}', encodedSignature)
.replace('{1}', expiry)
.replace('{2}', sas_name)
.replace('{3}', url);
console.log(auth);

输出

enter image description here

请引用ms docs

使用上面生成的SharedAccessSignature key 代替Authorization: SharedAccessSignature ${access key}的访问 key 。

除了访问 key 之外,我没有任何修改地使用了您的代码。我使用了上面生成的 key ,它给了我预期的结果。

enter image description here

关于azure - 如何从前端获取来自azure服务总线主题的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76659393/

相关文章:

java - Mockito等待新线程的方法执行多次

r - 向 Azure 服务总线队列发送消息和从 Azure 服务总线队列接收消息

c# - Azure Functions 服务总线触发器 : getting Serialization exception when trying to bind to custom class

azure - Pyspark - 数据透视表

azure - Azure ML 服务中的指标文档太大

Azure 存储投票模式

azure - 如何从 Azure 容器注册表中删除镜像

c# - 在 Azure 服务总线上调用 Abandon 会将消息重新排队到队列的后面而不是前面

c# - 从服务总线队列接收消息适用于控制台应用程序,但不适用于 Windows 窗体应用程序?

azure - Windows Azure 云服务未加载