如何使用Python验证Azure Runbook中的REST API



我正试图从自动化帐户中的python runbook访问配额列表/读取api,但我如何对其进行身份验证?

我在读书。我需要在头中传递一个承载身份验证令牌,但我发现的教程都是通过Postman完成的,这没有意义,因为这是一个内部运行的脚本。

API:https://learn.microsoft.com/en-us/rest/api/reserved-vm-instances/quota/list?tabs=HTTP

我在文档中发现了这一点,但看起来这是针对外部运行的python脚本的?https://learn.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-python-3

如有任何帮助,我们将不胜感激。

下面是一个在python api调用中传递头的基本示例。您可以在这里的headers变量中添加所需的headers,以成功调用api。

以下是关于python请求模块的文档。

import requests
# Make an API call and store the response
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f'Status Code: {r.status_code}')
# Store the response as a variable
response_dict = r.json()
print(f"Total repositories: {response_dict['total_count']}")

最新更新