【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码
问题描述
对于Azure资源进行配置操作,门户上可以正常操作。但是想通过Python代码实现,这样可以批量处理。那么在没有SDK的情况下,是否有快速办法呢?

问题解答
当然可以,Azure Portal上操作的所有资源都是通过REST API来实现的,所以只要找到正确的API,就可以通过浏览器中抓取到的请求Body/Header来实现转换为Python代码。
第一步:打开浏览器开发者模式(F12), 查看操作所发送的API请求
比如在操作对Resource group 进行Tags修改的时候,抓取到发送的请求为:https://management.chinacloudapi.cn/batch?api-version=2020-06-01, 所以把它的URL, Authorization,Payload内容都复制到文本编辑器中。

第二步:复制请求的Body/Header,特别是Authorization
从第一步发出的请求中复制的内容示例:
Host URL: https://management.chinacloudapi.cn/batch?api-version=2020-06-01
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6InpfMk...........
Payload:
{"requests":[{"content":{"operation":"Replace","properties":{"tags":{"test":"test","test1":"test2"}}},
"httpMethod":"PATCH","name":"xxxx-xx8","requestHeaderDetails":{"commandName":"HubsExtension.ArmTags.patchResourceTags"},
"url":"/subscriptions/xxxxxxxxxxxxx/resourceGroups/adls-rg/providers/Microsoft.Resources/tags/default?api-version=2019-10-01"}]}
复制好请求的Body,Header等信息后,组合成可以正确使用的URL, Authorization,Request Body。
URL 为 host + payload中的url ,拼接后的正确值是 :https://management.chinacloudapi.cn/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxx/resourceGroups/<resource group name>/providers/Microsoft.Resources/tags/default?api-version=2019-10-01
Body 内容为Payload中的content信息,所以是:{"operation":"Replace","properties":{"tags":{"test":"test","test1":"test2"}}}
第三步:在Postman等发送API的工具中测试请求是否成功,本处使用 VS Code 插件 Thunder Client
把第二步中的内容,填入到发送REST API的工具中验证,结果显示 200,修改成功。

第四步:转换为Python代码,并测试运行是否成功
在Thunder Client的Response窗口点击“{ }” 按钮,并选择Python 语言,复制示例代码。

Python示例代码(替换为正确的Access Token 和 SubscriptionID , Resource Group名称后,代码正常运行):
import http.client
import json conn = http.client.HTTPSConnection("management.chinacloudapi.cn") headersList = {
"Accept": "*/*",
"User-Agent": "Thunder Client (https://www.thunderclient.com)",
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJ.......",
"Content-Type": "application/json"
} payload = json.dumps({
"operation": "Replace",
"properties": {
"tags": {
"test": "test",
"test1": "test2"
}
}
}) conn.request("PATCH", "/subscriptions/xxxxxxxxx/resourceGroups/xxxx/providers/Microsoft.Resources/tags/default?api-version=2019-10-01", payload, headersList)
response = conn.getresponse()
result = response.read() print(result.decode("utf-8"))
第五步:用Python Code替换 hardcode Authorization
使用azure.identity来完成认证和显示获取AccessToken
from azure.identity import DefaultAzureCredential ##get access token
credential = DefaultAzureCredential()
accessToken = credential.get_token("https://management.chinacloudapi.cn/.default")
print(accessToken.token)
在结合第四步的Python代码后,就可以实现实时获取Access Token,并Python代码发送REST API.

完整示例代码:
import http.client
import json
from azure.identity import DefaultAzureCredential ##get access token
credential = DefaultAzureCredential() accessToken = credential.get_token("https://management.chinacloudapi.cn/.default") #print(accessToken.token) ## Send API
conn = http.client.HTTPSConnection("management.chinacloudapi.cn") headersList = {
"Accept": "*/*",
"User-Agent": "Thunder Client (https://www.thunderclient.com)",
"Authorization": "Bearer " +accessToken.token,
"Content-Type": "application/json"
} payload = json.dumps({
"operation": "Replace",
"properties": {
"tags": {
"test": "test",
"test1": "test2"
}
}
}) conn.request("PATCH", "/subscriptions/xxxxxxxxxxxxxx/resourceGroups/xxxxxxxx/providers/Microsoft.Resources/tags/default?api-version=2019-10-01", payload, headersList)
response = conn.getresponse()
result = response.read() print(result.decode("utf-8"))
参考资料
Thunder Client for VS Code : https://www.thunderclient.com/
【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码的更多相关文章
- 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...
- 【Azure Developer】通过Azure提供的Azue Java JDK 查询虚拟机的CPU使用率和内存使用率
问题描述 在Azure上创建虚拟机(VM)后,在门户上可以查看监控指标(Metrics),如CPU Usage,Memory,Disk I/O等.那如何通过Java 代码获取到这些指标呢? 关于VM ...
- 【Azure Developer】使用 Azure Python SDK时,遇见 The resource principal named https://management.azure.com was not found in the tenant China Azure问题的解决办法
问题描述 在使用Python SDK时候,登录到China Azure (Mooncake)并访问AlertsManagement资源时候,时常遇见 EnvironmentCredential: A ...
- 【Azure Developer】在Azure Resource Graph Explorer中查看当前订阅下的所有资源信息列表并导出(如VM的名称,IP地址内网/公网,OS,区域等)
问题描述 通过Azure的Resource Graph Explorer(https://portal.azure.cn/#blade/HubsExtension/ArgQueryBlade),可以查 ...
- 【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
问题描述 当本地环境中安装.NET 6.0后,用指令 dotnet new web 或 dotnet new console 生成的项目,使用的都是新模板生成的Program.cs文件.里面去掉了n ...
- 【Azure Developer】在Azure VM (Windows) 中搭建 kafka服务,并且通过本地以及远程验证 发送+消费 消息
问题描述 查看了 "How to Install and Run Apache Kafka on Windows? " 一文后,成功安装了Kafka服务,但是如何使用呢?如何在其他 ...
- 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID
问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...
- 【Azure Developer】Azure Graph SDK获取用户列表的问题: SDK中GraphServiceClient如何指向中国区的Endpoint:https://microsoftgraph.chinacloudapi.cn/v1.0
问题描述 想通过Java SDK的方式来获取Azure 门户中所列举的用户.一直报错无法正常调用接口,错误信息与AAD登录认证相关,提示tenant not found. 想要实现的目的,通过代码方式 ...
- 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式
问题描述 使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token. 如要了解如何从AAD中获取 client id,client secret,tenant id ...
- 【Azure Developer】使用 Microsoft Authentication Libraries (MSAL) 如何来获取Token呢 (通过用户名和密码方式获取Access Token)
问题描述 在上一篇博文<[Azure Developer]使用 adal4j(Azure Active Directory authentication library for Java)如何来 ...
随机推荐
- #KMP,dp#洛谷 3426 [POI2005]SZA-Template
题目 给定一个字符串\(S\),字符串可以理解成一条每个字母代表一种颜色的线段, 找到一个长度最小的串\(T\),使得在若干位置放置\(T\)后使得字符串被完全覆盖 分析 显然它要么取\(i\),要么 ...
- 共筑使能千行百业的数字底座 | HDC 2022松湖对话顺利召开
11月5日,华为开发者大会2022松湖对话在东莞松山湖凯悦酒店召开,开放原子开源基金会秘书长冯冠霖.华为终端BG软件部总裁龚体.深圳国家金融科技测评中心董事长钟剑.鸿湖万联(江苏)科技发展有限公司董 ...
- [IOI2000]邮局 题解
简要题意 线段上有 \(V\) 个村庄,现在要建 \(P\) 个邮局,使每个村庄到最近的邮局的距离之和最小. 50分做法 设\(dp[i][j]\) 表示第一个村庄到第 \(i\) 个村庄,建了 \( ...
- 深入理解 C# 编程:枚举、文件处理、异常处理和数字相加
C# 枚举 枚举是一个特殊的"类",表示一组常量(不可更改/只读变量). 要创建枚举,请使用 enum 关键字(而不是 class 或 interface),并用逗号分隔枚举项: ...
- 动态库 DLL 封装五:dll中弹出一个dialog窗口
操作步骤: 1.在dll项目中,点击 资源,新建一个 dialog 2.cpp文件 CDialog dlg(IDD_STA); // 显示窗口 dlg.Create(IDD_STA, 0); dlg. ...
- 一个很好用的ORM库--peewee
发现一个很好用的 ORM 库 -- peewee 以下为简单示例 from peewee import * db = SqliteDatabase('test.db') # 定义表结构 class P ...
- 编译 OpenCV 的 Python 依赖
这一次编译 OpenCV 的 Python 依赖为了方便运行我们使用 Docker 进行编译,环境准备如下: 系统依赖:Ubuntu 18.04 Python 版本:3.6,Ubuntu 18.04 ...
- stm32串口晶振不对输出乱码+汇承HC-14lora模块
最近要用到一个lora无线透传模块,然后就先用两个32开发板(用的STM32F103C8T6)试试简单的收发数据.结果,第一步串口发送一句话就寄了,我串口打印了"hi",结果出现了 ...
- 【Oracle】使用xmlagg(xmlparse(content()).getclobval()拼接信息
使用xmlagg(xmlparse(content()).getclobval()拼接信息 简单来说格式如下 xmlagg(xmlparse(content(内容||分割符)).getclobval( ...
- 力扣438(Java)-找到字符串中所有字母异位词(中等)
题目: 给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引.不考虑答案输出的顺序. 异位词 指由相同字母重排列形成的字符串(包括相同的字符串). 示例 1: ...