How to get device twin using Azure IoT Hub REST API on Android

wifi iot 1 Reputation point
2022-09-27T09:03:07.033+00:00

I want to get device twin by calling Azure IoT Hub REST API with Volley GET request on Android.
Can you provide sample code?
I've tried several things, but I get the following error code:

1) BasicNetwork.performRequest: Unexpected response code 401 for https://my-standard-iot-hub.azure-devices.net/twins/my-test-dev3?api-version=2020-05-31-preview
2) BasicNetwork.performRequest: Unexpected response code 500 for https://my-standard-iot-hub.azure-devices.net/twins/my-test-dev3?api-version=2020-05-31-preview

I look forward to your help.

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

1 answer

Sort by: Most helpful
  1. chbeier 1,866 Reputation points
    2022-09-27T14:04:38.427+00:00

    Hey @wifi iot ,

    You can easily test the Azure IoT Hub Service REST API with Postman or others. It is important to use the right authentication against IoT Hub, here are the options listed iot-hub-devguide-security
    For accessing the Service API and Device Twins as part of the Device Registry, you might want to use either Azure AD or a Shared Access Signature (SAS).

    If you use the SAS, HTTPS implements authentication by including a valid token in the Authorization request header. To generate a SAS for your IoT Hub, you can use Visual Studio Code IoT Hub Tools, Azure CLI with IoT extension az iot hub generate-sas-token or one of the code samples provided in the docs iot-hub-dev-guide-sas. Ensure to use the right policy (IoTHubOwner will do it - you might want to limit access by using a different policy).

    1 person found this answer helpful.