使用设备客户端和设备预配置服务(使用HTTP DELETE)在Azureure IoT中心中删除(取消配置)设备

时间:2020-10-08 05:22:19

标签: azure azure-iot-hub azure-iot-sdk

什么原因导致尝试使用DPS从azure IoT中心删除设备时导致未授权的问题:

Documentation

Documentation for delete operation using DPS in azure iot hub

我尝试取消配置设备,但也收到错误401或405。授权标头中使用的sas令牌的格式是什么:

生成sas令牌的代码:

public String buildSasToken1(String deviceId) throws SecurityProviderException {

        String keyValue = Utils.getKeyValue(deviceId, iotConfig.getSymmetricKey());
        Long expiry = getExpiry(iotConfig.getExpiry());
        try {
            String dpsUrl=iotConfig.getHubUrl1();
            String targetUri = URLEncoder.encode(dpsUrl.toLowerCase(), StandardCharsets.UTF_8.name());
            String toSign = targetUri + "\n" + expiry;
            byte[] keyBytes = Base64.decodeBase64Local(keyValue.getBytes("UTF-8"));
            SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(toSign.getBytes("UTF-8"));
            
            String signature = URLEncoder.encode(Base64.encodeBase64StringLocal(rawHmac), "UTF-8");
            String token = String.format(iotConfig.getTokenFormat(), targetUri, signature, expiry)+"&skn=provisioningserviceowner";
            log.info("TOKEN "+token);
            return token;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException(e);
        }
    }

我附上了在PostMan中显示请求的图片:

enter image description here

enter image description here

0 个答案:

没有答案