Azure/azure-sdk-for-python

Error using DefaultAzureCredential with TableServiceClient

russd2357 opened this issue · 4 comments

Changing code to move away from using secrets to identity.

To Reproduce
Steps to reproduce the behavior:

  1. Create a CosmosDB account with Table API enabled
  2. Log into Azure using az login (using account with Owner privilege on the subscription)
  3. run this python code:
   try:
        creds = DefaultAzureCredential()
        table = TableServiceClient(
            endpoint=f"https://{cosmosdb_account_name}.table.cosmos.azure.com:443/",  
            credential=creds).get_table_client(table_name=cosmosdb_table)
        
        messageProcessingTime = datetime.utcnow() - datetime.strptime(jsonMessage["srcStamp"],date_format) 
        print(f'messageProcessingTime: {messageProcessingTime.total_seconds()}')

        // hydrate an entity for insertion into table
        
        response = table.create_entity(entity=entity)
        
        print (f"insert_entity response timestamp {str(response)}")
    except Exception as error:
        print(f"Error has happened : {error}")

**Expected behavior**
The table should be updated.  This code works just fine when I use a connection string to construct the TableServiceClient.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Additional context**
This is the error message I get:   

Operation returned an invalid status 'Unauthorized'
Content: {"odata.error":{"code":"Unauthorized","message":{"lang":"en-us","value":"Authorization header doesn't confirm to the required format. Please verify and try again.\r\nActivityId: 0ad768a3-5ad9-4b23-8982-ce8bb4c14073, documentdb-dotnet-sdk/2.14.0 Host/64-bit MicrosoftWindowsNT/10.0.20348.0\nRequestID:0ad768a3-5ad9-4b23-8982-ce8bb4c14073\n"}}}

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @klaaslanghout.

Thanks for reaching out.

Could you enable logging and share the logs?

import logging
logging.basicConfig(level=logging.DEBUG)

Hi @russd2357. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.