Troubleshooting Error when Updating Device State from Disabled to Enabled Using iothub SDK Registry.fromConnectionString

KURAMOTOAHUJATAIRA-3118 250 Reputation points
2024-01-29T09:11:31.12+00:00

I'm encountering the following error while attempting to update a device's state from disabled to enabled using the iothub SDK's Registry.fromConnectionString. This error didn't occur previously, and I'm uncertain about its cause. Any advice would be appreciated. Error:

2024-01-29T08:40:08Z   [Error]   2024-01-29T08:40:08+00:00 [ERROR] getIothubSakey test 500 ArgumentError: authentication is invalid.
    at RestApiClient.translateError (C:\home\site\wwwroot\node_modules\azure-iothub\node_modules\azure-iot-http-base\dist\rest_api_client.js:261:25)
    at requestCallback (C:\home\site\wwwroot\node_modules\azure-iothub\node_modules\azure-iot-http-base\dist\rest_api_client.js:183:40)
    at IncomingMessage.<anonymous> (C:\home\site\wwwroot\node_modules\azure-iothub\node_modules\azure-iot-http-base\dist\http.js:117:17)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Code:

async function updateDevice(deviceId) {
  const registryReadWriteRegistry = iothub.Registry.fromConnectionString(
      systemConfig.iothub.registryReadWriteConnectionStr,
  );
  const deviceInfo = await registryReadWriteRegistry.get(deviceId);
  deviceInfo.responseBody.status = "enabled";
  await registryReadWriteRegistry.update(deviceInfo.responseBody);
}
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,127 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 13,471 Reputation points
    2024-02-06T16:42:35.0133333+00:00

    Hi @KURAMOTOAHUJATAIRA-3118 Thank you so much for sharing the solution identified by the Azure team. I am sharing the solution below for easier access on the Community thread. Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer .

    Error Message:

    2024-01-29T08:40:08Z [Error] 2024-01-29T08:40:08+00:00 [ERROR] getIothubSakey test 500 ArgumentError: authentication is invalid.

    Issue:

    Attempting to update a device's state from disabled to enabled using the IoTHub SDK's Registry.fromConnectionString results in an error

    Solution:

    Azure team confirmed that the issue occurs with IoT Hub resources that have undergone the "Gateway Infrastructure Upgrade" mentioned in the following update announcement:

    Reference: General availability: IoT Hub Service Upgrade https://azure.microsoft.com/en-us/updates/general-availability-iot-hub-service-upgrade/

    In the Device class used in the code, it was found that there were values containing properties SymmetricKey and symmetricKey, which could be considered duplicates, in the authentication section. It was noted that if the operation proceeded with this definition as is, errors would be returned due to stricter validation of API request content in IoT Hub resources that have undergone the upgrade process.

    Reference: Authentication interface https://learn.microsoft.com/en-us/javascript/api/azure-iothub/device.authentication?view=azure-node-latest

    As a solution to this issue, it was suggested to handle the update method using an object excluding only the inappropriate SymmetricKey field. The code example is as follows: If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information.

    async function updateDevice(deviceId) {
        const registryReadWriteRegistry = iothub.Registry.fromConnectionString(
            systemConfig.iothub.registryReadWriteConnectionStr,
        );
        const deviceInfo = await registryReadWriteRegistry.get(deviceId);
        deviceInfo.responseBody.status = 'enabled';
        const {responseBody: {authentication: {SymmetricKey, ...rest}, ...outerRest}} = deviceInfo;
        const newDeviceInfo = {
            ...outerRest,
            authentication: {...rest}
        };
        await registryReadWriteRegistry.update(newDeviceInfo);
    }
    

    I hope this helps!

    If you have any other questions, please let me know. Thank you again for your time and patience throughout this issue.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sander van de Velde 29,196 Reputation points MVP
    2024-01-30T17:14:12.06+00:00

    Hello @KURAMOTOAHUJATAIRA-3118,

    The code example you use is custom code. I have no clue what is happening there.

    Can you please check these lines of C# code which use the basic SDK?

    	using Microsoft.Azure.Devices;
    	internal class Program
    	{
    		static async Task Main(string[] args)
    		{
    			var cs = "HostName=XYZ.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=XYZ";
    
    			var registryReadWriteRegistry = RegistryManager.CreateFromConnectionString(cs);
    
    			await registryReadWriteRegistry.OpenAsync();
    
    			var  device = await registryReadWriteRegistry.GetDeviceAsync("pr");
    
    			device.Status = DeviceStatus.Disabled;
    
    			await registryReadWriteRegistry.UpdateDeviceAsync(device);
    		}
    	}
    
    

    Notice you need to add the 'Microsoft.Azure.Devices' nuget package.

    It works correctly for an IoT Hub in West Europe.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.