gpt4 book ai didi

postman - 如何在 Postman 中创建 SAS token 以与服务总线一起使用?

转载 作者:行者123 更新时间:2023-12-05 01:27:30 26 4
gpt4 key购买 nike

基于此 Microsoft 文档: https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token

nodejs 的代码是:

function createSharedAccessToken(uri, saName, saKey) { 
if (!uri || !saName || !saKey) {
throw "Missing required parameter";
}
var encoded = encodeURIComponent(uri);
var now = new Date();
var week = 60*60*24*7;
var ttl = Math.round(now.getTime() / 1000) + week;
var signature = encoded + '\n' + ttl;
var signatureUTF8 = utf8.encode(signature);
var hash = crypto.createHmac('sha256', saKey).update(signatureUTF8).digest('base64');
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName;
}

我在 Postman 上尝试过,以防有人需要帮助。这是使用 Postman 在预请求脚本下为我工作的代码

var namespace = pm.collectionVariables.get("serviceNamespace");
var uri = "https://" + namespace + ".servicebus.windows.net/adfTest/messages";
var saName = pm.collectionVariables.get("SharedAccessKeyName");
var saKey = pm.collectionVariables.get("SharedAccessKeySend");

var sasToken = createSharedAccessToken(uri, saName, saKey);

console.log(sasToken);
pm.collectionVariables.set("SasToken", sasToken);

function createSharedAccessToken(uri, saName, saKey) {
if (!uri || !saName || !saKey) {
throw "Missing required parameter";
}
var encoded = encodeURIComponent(uri).toLowerCase();
var now = new Date();
var week = 60*60*24*7;
var ttl = Math.round(now.getTime() / 1000) + week;
var signature = encoded + '\n' + ttl;
var hash = CryptoJS.HmacSHA256(signature, saKey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
encodeURIComponent(hashInBase64) + '&se=' + ttl + '&skn=' + saName;
}

最佳答案

  1. 在您的集合下创建变量: enter image description here

从 Azure 门户获取值 - 服务总线服务命名空间、SharedAccessKey、SharedAccessKeyName

  1. 在Pre-request Script下添加代码 enter image description here

    var namespace =      pm.collectionVariables.get("serviceNamespace");
    var uri = "https://" + namespace + ".servicebus.windows.net/adfTest/messages";
    var saName = pm.collectionVariables.get("SharedAccessKeyName");
    var saKey = pm.collectionVariables.get("SharedAccessKeySend");

    var sasToken = createSharedAccessToken(uri, saName, saKey);

    console.log(sasToken);
    pm.collectionVariables.set("SasToken", sasToken);

    function createSharedAccessToken(uri, saName, saKey) {
    if (!uri || !saName || !saKey) {
    throw "Missing required parameter";
    }
    var encoded = encodeURIComponent(uri).toLowerCase();
    var now = new Date();
    var week = 60*60*24*7;
    var ttl = Math.round(now.getTime() / 1000) + week;
    var signature = encoded + '\n' + ttl;
    var hash = CryptoJS.HmacSHA256(signature, saKey);
    var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
    return 'SharedAccessSignature sr=' + encoded + '&sig=' +
    encodeURIComponent(hashInBase64) + '&se=' + ttl + '&skn=' + saName;
    }

注意:adfTest部分为主题名称,应替换为您的主题名称

var uri = "https://" + namespace + ".servicebus.windows.net/adfTest/messages";
  • 在授权下使用 sasToken 变量: enter image description here

  • 从下拉菜单中选择 API key 。

  • key =授权

  • Value = {{SasToken}} <-- 这是变量名

  • 添加到 = 标题

  1. 创建帖子: enter image description here

这些是职位的要求

     POST https://<yournamespace>.servicebus.windows.net/<yourentity>/messages
Content-Type: application/json
Authorization: SharedAccessSignature sr=https%3A%2F%2F<yournamespace>.servicebus.windows.net%2F<yourentity>&sig=<yoursignature from code above>&se=1438205742&skn=KeyName
ContentType: application/atom+xml;type=entry;charset=utf-8

URL 应如下所示:https://.servicebus.windows.net//消息

在授权下从父级继承授权: enter image description here

这将在集合下添加带有预请求脚本生成的 SAS token 的授权 header

添加其他标题: enter image description here

  1. 将消息放在正文中: enter image description here

这可以是任何 json 数据

关于postman - 如何在 Postman 中创建 SAS token 以与服务总线一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69383150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com