我如何从Python脚本作为用户进行验证?

发布于 2025-01-30 20:24:51 字数 1019 浏览 1 评论 0原文

我有一个Python脚本,应该使用链接从Web资源下载数据。碰巧的是,资源是在Active Directory保护的Azure App服务中。我的用户帐户可以访问链接并下载数据(我可以手动从Web浏览器手动执行此操作,但要自动化此过程)。 Python脚本使用请求库。我无法弄清楚如何正确身份验证,因为当我尝试运行脚本时,我会得到:

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

使用请求不起作用的常规身份验证(使用auth参数或session.auth或使用httpntlmauth)。 我知道可以使用VS代码对Azure进行身份验证,然后使用DefaultazureCrecredential,但是我无法在哪里使用此defaultazurecrecrecrecrecredential object(因为它不起作用)使用请求)。

我不需要整个Python应用程序被注册或以某种方式通过Azure资源识别。这只是下载数据的脚本,不应生产数据。

有什么想法,我如何从Azure中删除数据?

注意:我不是这个Azure应用程序的管理员或创建者,因此无法更改任何限制设置。

简而言之,脚本提出请求的一部分看起来像:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2025-02-06 20:24:51

如果要访问Azure应用服务,则必须对Azure应用程序服务进行身份验证。如果您无法访问Azure App Service,我们将无法访问Azure资源。

当Web服务器阻止您访问要在浏览器中打开的页面时,您会遇到403禁止错误。大多数时候,您无法做很多事情。但是,有时候问题在您结束时。
这是一些可能导致此错误的点。

  • 如果您有开放的公共API,并且在Azure App Service上不允许公共访问。
  • 您使用的IP地址是您用于调用应用程序服务的IP地址。
  • 如果您的中间有门户,则可能还可以阻止您的呼叫。

这是您可以尝试的可能解决方案:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文