我们使用 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);
输出
请引用ms docs
使用上面生成的SharedAccessSignature key 代替Authorization: SharedAccessSignature ${access key}
的访问 key 。
除了访问 key 之外,我没有任何修改地使用了您的代码。我使用了上面生成的 key ,它给了我预期的结果。
关于azure - 如何从前端获取来自azure服务总线主题的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76659393/