首页 > 解决方案 > 使用设备客户端和使用 HTTP DELETE 的设备预配服务在 azure iot hub 中删除(取消预配)设备

问题描述

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

文档

在 azure iot hub 中使用 DPS 进行删除操作的文档

我尝试了取消配置设备,但也收到错误 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 中显示请求的图片:

在此处输入图像描述

在此处输入图像描述

标签: azureazure-iot-hubazure-iot-sdk

解决方案


推荐阅读