我要使用邮政编码在Azure IoT集线器中创建用于注册设备的SAS令牌.SAS令牌将使用预先请求的脚本创建.
var resourceUri = "scopeId/registrations/deviceId" // The resource uri
var deviceId = "deviceId";
resourceUri = encodeURIComponent(resourceUri.toLowerCase()); // Encode the url
var expires = Math.ceil((Date.now() / 1000) + 10 * 60); // Expire the token 60 minutes from now
var toSign = resourceUri + "\n" + expires; // this is the string format to gen signature from
var crypted = CryptoJS.HmacSHA256(deviceId, CryptoJS.enc.Base64.parse("symmetrickKeyOfEnrollmentGroup"));
var signature = CryptoJS.HmacSHA256(toSign, crypted); // The signature generated from the decodedKey
var encodedUri = encodeURIComponent(CryptoJS.enc.Base64.stringify(signature)); // The url encoded version of the Base64 signature
// Construct authorization string (shared access signature)
var iotHubSasToken = "SharedAccessSignature sr=" + resourceUri + "&sig=" + encodedUri + "&se=" + expires +"&skn=registration";
console.log(iotHubSasToken);
postman.setGlobalVariable("token", iotHubSasToken);
这是我创作的,但我收到的是未经授权的.有些 idea 我错了,我想我在签名的某个地方,因为"sr"和"se"是可以的
上面抛出未经授权的代码的输出:
SharedAccessSignature sr=0ne002ee24e%2Fregistrations%2Fcxdlx3f3zv9xx3f3zq&sig=Ukz%2FPyyLaweLYmFq4gHUP%2BhiO7X%2FyQAE9noAaw4nuLU%3D&se=1659940252&skn=registration
参考资料:
关于SAS:https://docs.microsoft.com/en-us/azure/iot-dps/how-to-control-access
关于DPS SAS令牌:https://docs.microsoft.com/en-us/azure/iot-dps/how-to-control-access
错误: