Delete (deprovision) device in azure iot hub using device client and device provisioning service using HTTP DELETE

238 views Asked by At

What causes this problem of getting Unauthorized when trying to delete a device from azure IoT hub using the DPS:

Documentation

Documentation for delete operation using DPS in azure iot hub

I tried deprovision device but also getting error 401 or 405. What is the format for the sas token which is used in the authorization header:

Code which generate the sas token:

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);
        }
    }

I attach pictures which are showing the request in PostMan:

enter image description here

enter image description here

0

There are 0 answers