Microsoft azure login python

I’m testing the Microsoft login azure sample code (GitHub - Azure-Samples/ms-identity-python-webapp: A Python web application calling Microsoft graph that is secured using the Microsoft identity platform) but it gives me an error:

Almost there. Did you forget to set up necessary environment variables for your deployment?

I have added the client id and secret of the azure application but it is as if it did not take that data. The code is as is, I have simply added the id’s

.env.sample

# Note: If you are using Azure App Service, go to your app's Configuration,
# and then set the following values into your app's "Application settings".
CLIENT_ID='430e8cf741f'
CLIENT_SECRET='Y3E8Q~PZZgY5a0CSc1X'
# The AUTHORITY variable expects a full authority URL.
#
# If you are using an AAD tenent, configure it as
# "https://login.microsoftonline.com/TENANT_GUID"
# or "https://login.microsoftonline.com/subdomain.onmicrosoft.com".
#
# If you are using a CIAM tenant, configure it as "https://subdomain.ciamlogin.com"
#
# Alternatively, leave it undefined if you are building a multi-tenant app in world-wide cloud
AUTHORITY='892241-08e56825da42'

Why does it keep asking me for the secret, id?

I’m assuming that those are the sample values, since you show a file called .env.sample . At least, I’m hoping that you haven’t just published your actual IDs - if you have, go and change them now.

You need to have those as actual environment variables. Having a file called .env is a common convention, but it isn’t automatic. Often what you then need to do is source .env in your shell, but depending on how you’re set up, that might be written differently.

the ids are not the good ones, they are invented.
I have done the test and putting the values directly in app_config.py if it works.
So what happens is that app_config.py doesn’t pick them up from the .env.sample file
I have not understood very well what you are telling me.
I have created an environment following the command

py -m venv .venv

later I have activated and executed app.py

Okay, good :slight_smile:

Also good.

It shouldn’t. What it will be looking for is a file called .env - the sample is just there as, well, a sample. Try copying that to .env and seeing whether it now reads those variables.

Unfortunately, the name “environment” has many meanings, but the Python “Virtual Environment” has nothing to do with “environment variables” which are the problem here.

At the moment it doesn’t work for me… but I happen to have another problem.
Previously, I had a multi-tenant position and now I have a single tenant position.
Well it gives me an error

invalid_client
AADSTS700025: Client is public so neither 'client_assertion' nor 'client_secret' should be presented. Trace ID: 0f0600 Correlation ID: 0a2cf92d Timestamp: 2023-08-01 11:38:36Z

I don’t know if I’m putting the data where it should be
AUTHORITY = "https://login.microsoftonline.com/<tenant id>"

is it so?

Ah, that’s an Azure-specific problem, so I’ve no idea, sorry.

Finally I solved the azure issue but continuing with the thread of the main problem, I don’t finish making it work, it still doesn’t take the values of the .env file from app_config.py

I have the .env file like this:

.env

CLIENT_ID=666hh7j7ju
CLIENT_SECRET=5675ytjhui789797

At the moment I have it put directly in the app_config.py file so that it works for me but it would be in the .env file

The .env file is intended for local development. On production, you should set those environment variables on App Service, in Application Settings.

the tests are locthe tests are local, for the momental, for the moment

Ah okay, did you pip install -r requirements.txt? It includes python-dotenv, and Flask only picks up env values from .env is python-dotenv is installed.

Yes, the package is installed.