Getting 401 on REST call to IoTHub (on 'devicebound')

Aurovind Sagar Epari 95 Reputation points
2023-08-16T13:00:50.5833333+00:00

Hi Team,

I want to send C2D from a web page. I am able to make REST call to IoTHub on 'events' but not on 'devicebound'. I am getting unauthorized 401 when I post on 'devicebound'. On 'events', I get 204 as expected and I see my message on built-in endpoint.

Note: Just to be sure I have created SAS token with iothubowner policy. Am I missing anything? I believe the same SAS token should work for both, right? Maybe, there is gap in my understanding.

My REST code,

const response = await fetch(`https://myhub.azure-devices.net/devices/device_xyz/messages/devicebound?api-version=2020-03-13`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': sasToken
                    },
                    body: message
                });
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
0 comments No comments
{count} vote

Accepted answer
  1. LeelaRajeshSayana-MSFT 17,776 Reputation points Moderator
    2023-08-16T18:29:49.3533333+00:00

    Hi @Aurovind Sagar Epari Greetings! Thank you for question here.

    I would like to point out that the API end point has a GET method implementation and not POST. Please refer the documentation - Device - Receive Device Bound Notification for more information.

    I believe the same SAS token should work for both, right?

    To answer this question, I would like to make a note that each shared access policy comes with a different set of permissions to it. For more information on this, please find the below details which can be found from IoT hub-level shared access policies

    User's image

    Since the API reads cloud-to-device messages, Device shared access policy should work in trying to fetch the result. You wouldn't explicitly need iothubowner policy credentials.

    May I also know the steps you have followed to create the SAA token? Please find the below code for reference that would help guide you in generating the token with the correct format.

    var generateSasToken = function(resourceUri, signingKey, policyName, expiresInMins) {
        resourceUri = encodeURIComponent(resourceUri);
    
        // Set expiration in seconds
        var expires = (Date.now() / 1000) + expiresInMins * 60;
        expires = Math.ceil(expires);
        var toSign = resourceUri + '\n' + expires;
    
        // Use crypto
        var hmac = crypto.createHmac('sha256', Buffer.from(signingKey, 'base64'));
        hmac.update(toSign);
        var base64UriEncoded = encodeURIComponent(hmac.digest('base64'));
    
        // Construct authorization string
        var token = "SharedAccessSignature sr=" + resourceUri + "&sig="
        + base64UriEncoded + "&se=" + expires;
        if (policyName) token += "&skn="+policyName;
        return token;
    };
    
    
    

    Lastly, please ensure that you are calling this end point to an IoT Hub on Standard tier because this capability is only available in the standard tier IoT Hub.

    Please let us know if you have any further questions or need additional information.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.