python - 函数/主机 key 是否可以在用 Python 编写的 Azure 函数中直接使用?

标签 python azure azure-functions

在使用 Python 编写 Azure 函数时,我希望能够从环境中访问主机和函数 key 。这可能吗?我见过的所有示例都是通过调用 get 请求来实现的,这似乎需要大量代码来访问我通过网站设置的内容。

This question非常相似,但不特定于语言。

最佳答案

听起来您想获得Azure Functions的Host API admin/host/keys的响应,如下所示,所以请引用Azure Functions wiki页面Key management API

enter image description here

这是我的示例代码。

# App Credentials, to get it see the figures below
username = "<your username like `$xxxxx`>"
password = "<your password>"

functionapp_name = "<your function app name>"
api_url = f"https://{functionapp_name}.scm.azurewebsites.net/api"
site_url = f"https://{functionapp_name}.azurewebsites.net"

import base64
import requests

auth_info = f"{username}:{password}"

base64_auth = base64.b64encode(str.encode(auth_info)).decode()
print(base64_auth)

jwt_resp = requests.get(f"{api_url}/functions/admin/token", headers={"Authorization": f"Basic {base64_auth}"})
jwt = jwt_resp.text.replace("\"", "", -1)
print(jwt)

keys_resp = requests.get(f"{site_url}/admin/host/keys", headers={"Authorization": f"Bearer {jwt}"})
print(keys_resp.text)

它的工作原理及其结果如下。

enter image description here

获取App Credentials的用户名和密码,请参见下图。

图 1. 在 Azure 门户上,打开 Function App 的平台功能选项卡,然后单击部署中心链接

enter image description here

图2.在SOURCE CONTROL第一步选择FTP选项,点击Dashboard按钮复制的值>用户名密码,但只需使用用户名中带有$前缀的部分作为用户名变量在我的脚本中。当然,您也可以在用户凭据选项卡中使用它们。

enter image description here

另外,您可以引用我对类似SO线程的回答Unable to access admin URL of Azure Functions使用 PowerShell,我下面的数据就来自于此。

<小时/>

更新:要在容器中使用 Azure Function for Python,请引用下图获取部署凭据。

enter image description here

关于python - 函数/主机 key 是否可以在用 Python 编写的 Azure 函数中直接使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58871693/

相关文章:

python - 将行添加到尚不存在的 DataFrame 列

python - 在 Python 3 中确定类的元类

python - python中的最大行长度

python - 如何通过多列函数对 Pandas 行进行分组

azure - DocumentDB 按结果排序

Azure SendGrid DeliverAsync 可以工作,但不能交付

azure - 用户无权执行操作“Microsoft.Resources/deployments/validate/action”

azure - 宇宙数据库 : Are changes ever deleted from Change Feed?

azure - 从 Azure Functions 调用 Microsoft Graph API

c# - Azure 函数 - 队列触发器将 DateTimeOffset 返回为 null