'How can I authenticate as a user to Azure App Service from Python script?

I have a Python script that should download data from a web resource using link. It so happened that resource is in Azure App Service protected by Active Directory. My user account is allowed to access the link and download data (I can do it from web browser manually, but want to automate this process). The Python script uses requests library. I can't figure out how to authenticate properly, cause when I'm trying to run the script, I get:

Error 403 - Forbidden
The web app you have attempted to reach has blocked your access.

Usual authentication with requests doesn't work (using auth parameter or session.auth or with HttpNtlmAuth). I know one can use VS Code to authenticate to Azure and then use DefaultAzureCredential, but I can't get where you should use this DefaultAzureCredential object (cause it doesn't work with requests).

I don't need the whole Python app to be registered or somehow else recognizable by Azure resource. It's just a script to download data, that is not supposed to be productionized.

Any ideas how I can scrap the data from Azure?

Note: I'm not an admin or creator of this Azure App, so can't change any restriction settings.

In short, the part of script making request looks like:

params = {"param1": param1,
          "param2": param2}
session = requests.Session()
session.auth = HttpNtlmAuth(USERNAME, PASSWORD)
url = "my-app.azurewebsites.net/the-rest-of-the-path"
response = session.get(url, params=params, verify=False)


Solution 1:[1]

If you want to access the Azure App Service, you have to authenticate the Azure App Service. If you don't have access for Azure App Service, we cannot access the Azure resources.

Genereally, when a web server stops you from accessing the page you're trying to open in your browser, you'll get a 403 Forbidden Error. There isn't much you can do most of the time. However, occasionally the issue is on your end. Here are some points that can cause this error.

  • If you have an open public API and public access is not allowed on Azure App Service.
  • Your app's IP address, which you're using to call the app service, isn't whitelisted.
  • If you have a gateway in the middle, it's possible that it's also blocking your calls.

Here are the possible solutions that you can try:

  • Remove the access restrictions from your web app's Networking page.

  • Try adding 0.0.0.0/0 to give access to all. You can later add restrictions based on your needs.

  • The order of the restrictions is important, so double-check it. It may have an impact if you have a blocked call before an approved call.

  • You can also have restrictions based on http-headers like X-Forwarded-For. Please double-check that. This can also happen in code, depending on how you handle errors. Protocol support for HTTP headers in Azure Front Door | Microsoft Docs

  • Chech this, if your API is behind the Gateway Application Gateway integration with service endpoints

Solution 2:[2]

I was using fasterxml ObjectMapper to deserialize the object https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html

And after few tests I realized that it is able to deserialize primitive datatypes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SuryasriKamini-MT
Solution 2 purduche90