So I'm having all sorts of problems trying to authenticate a Python environment. My ultimate goal here is to use the Azure AppConfiguration SDK to read values from an AppConfig service in Azure. But right now I'm failing at the very first hurdle.
FYI I'm running all of this on a Windows 11 machine using VS Code (run as Administrator). I have the latest Azure CLI installed.
My Azure App Config script is as follows:
from azure.identity import DefaultAzureCredential
from azure.appconfiguration import AzureAppConfigurationClient
appCfgUrl = os.environ["AppConfigUrl"]
credential = DefaultAzureCredential()
client = AzureAppConfigurationClient(appCfgUrl, credential)
value = client.get_configuration_setting(configKey)
But this results in the error:
DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
EnvironmentCredential: Authentication failed: AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope https://Endpoint=https://foo.azconfig.io;Id=XXXXXX;Secret=YYYYYY=/.default is not valid.
So I thought maybe there was something wrong with the AppConfig SDK, or the way I'm implementing it, so I thought I'd try going direct and just connecting straight to the Azure CLI directly using the following code as suggested by Authenticating Azure CLI with Python SDK
import os
from azure.cli.core import get_default_cli
az_cli = get_default_cli()
clientId = os.environ["AZURE_CLIENT_ID"]
clientSecret = os.environ["AZURE_CLIENT_SECRET"]
tenantId = os.environ["AZURE_TENANT_ID"]
az_cli.invoke(['login', '--service-principal',
'-u', clientId,
'-p', clientSecret,
'--tenant',tenantId])
However, this just results in the error message
No module named 'azure.cli.command_modules'.
'login' is misspelled or not recognized by the system.
I've tried both pip install azure.cli.core as well as pip install azure-cli
I've also upgraded the Azure CLI on my machine. I can successfully authenticate to the Azure CLI in both Bash and PowerShell without any issues - and I have another .NET C# project which uses Azure CLI without problems.
At this point I'm really tearing my hair out. I've tried using hard-coded values for the service principal, as well as using locally defined Environment Variables - but none of them work.
As mentioned, I've done all of this in .NET in both Unit Tests, an Azure Function App and an Azure Web App without any issues at all, but doing this in Python appears to have just hit a hard stop.