azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'Forbidden'

Juanhe Shen 50 Reputation points
2023-11-29T20:49:14.2766667+00:00

I ran into a problem which involves obtaining data from my Azure digital Twin using the python script in Blender. I am currently trying to obtain some temperature value from one of my iot devices and retrieve it in my blender application and get it to show in the software. Thus, what I have here is my ADT_ENDPOINT_URI (my digital twin host name or the Azure digital twin explorer website), then I created an application in App registration for Blender, and got my tenant_ID, Client_ID, and also the client secret value. I set all of the above as my credential for the script to have access. Meanwhile, in the role assignment, I have the following roles: Azure Digital Twin data, Contributor, Owner, and App Configuration Data. Then I ran the script, and the console shows the error message: azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'Forbidden'

However, before the error, I also have: INFO:azure.identity._internal.get_token_mixin:ClientSecretCredential.get_token succeeded. Now after this, I have INFO:azure.core.pipeline.policies.http_logging_policy:Response status: 403, which occurred after the message: A body is sent with the request, in which the request headers are as follow: 'Content_type', 'Content- Length', 'Accept':'application/json', request id and user-agent. The Authorization is shown as 'REDACTED'. What could be the cause of this error.

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
232 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 16,946 Reputation points
    2023-11-30T03:25:58.3766667+00:00

    Hi @Juanhe Shen Thank you for posting the question on this forum. Have you looked into the code sample from Python SDK dt_digitaltwins_get.py The sample provides a reference on how to get the Digital Twin data using Python. I have tried the same code and could extract a single property of the Digital Twin. Please refer the below code sample change I have added to the GitHub code to extract a single property from the returned Digital Twin dictionary.

    import os
    import sys
    import logging
    from azure.identity import DefaultAzureCredential
    from azure.core.exceptions import HttpResponseError
    from azure.digitaltwins.core import DigitalTwinsClient
    
    try:
    
        url = os.getenv("AZURE_URL")
    
        # DefaultAzureCredential expects the following three environment variables:
        # - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
        # - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
        # - AZURE_CLIENT_SECRET: The client secret for the registered application
        credential = DefaultAzureCredential()
    
        # Create logger
        logger = logging.getLogger('azure')
        logger.setLevel(logging.DEBUG)
        handler = logging.StreamHandler(stream=sys.stdout)
        logger.addHandler(handler)
    
        # Create service client and enable logging for all operations
        service_client = DigitalTwinsClient("https://<ADTInstanceName>.api.eus.digitaltwins.azure.net", credential)
    
    
        digital_twint_id = "ReadingTank"
    
        # Get twin
        digital_twin = service_client.get_digital_twin(digital_twint_id)
    
        print("Starting to print digital twin")
        print(digital_twin)
        print('Extracting level of material')
        for key, value in digital_twin.items():
            if key == 'Level_of_Material':
                print(value)
    
    except HttpResponseError as e:
        print("\nThis sample has caught an error. {0}".format(e.message))
    
    

    For testing purposes I have directly provided the ADT instance URL in the line of the code where the service_client is initialized. I have also removed the function overload logging_enable=True. I could get the twin data as expected and could extract the value. Please refer the below image for reference.

    enter image description here

    Hope this helps. Please let us know if you have any additional questions on this.


    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.


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.