【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)如何来 ...
随机推荐
- 知识汇总:查看linux服务器系统命令
要查看Linux服务器的系统信息,你可以使用多种命令来获取不同类型的信息.以下是一些常 用的命令和它们的用途: uname - 显示基本的系统信息 uname -a:显示所有的系统信息,包括内核名称. ...
- Docker学习路线1:介绍
Docker是什么? Docker是一个开源平台,通过将应用程序隔离到轻量级.可移植的容器中,自动化应用程序的部署.扩展和管理.容器是独立的可执行单元,封装了运行应用程序所需的所有必要依赖项.库和配置 ...
- Discovery直播 | 移动应用“通行证”——钥匙环,解锁管家式安全出行服务
用户在登录环节的直接诉求是:别让我等.别让我想.别让我烦.而帐号输入.繁琐验证,以及由此带来的安全风险,总会让很多人望而却步. 如何在简化登录流程的同时保障登录凭证安全?如何帮助用户一键免密登录同一开 ...
- leetcode:763. 划分字母区间
763. 划分字母区间 字符串 S 由小写字母组成.我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段.返回一个表示每个字符串片段的长度的列表. 示例 1: 输入: S = & ...
- 无缝衔接 gRPC 与 dubbo-go
最近我们 dubbo-go 社区里面,呼声很大的一个 feature 就是对 gRPC 的支持.在某位大佬的不懈努力之下,终于弄出来了. 今天我就给大家分析一下大佬是怎么连接 dubbo-go 和 g ...
- 支付宝移动端 Hybrid 解决方案探索与实践
支付宝 Hybrid 方案建设与演进 目前支付宝有 2 套 Hybrid 方案: HTML5 容器与小程序.小程序是最近几年才出来,H5 容器已经有了很长时间的历史,所以我们就先从 H5 容器说起. ...
- OpenYurt 之 Yurthub 数据过滤框架解析
简介:OpenYurt 是业界首个非侵入的边缘计算云原生开源项目,通过边缘自治,云边协同,边缘单元化,边缘流量闭环等能力为用户提供云边一体化的使用体验.在 Openyurt 里边缘网络可以使用数据过滤 ...
- Flink 如何实时分析 Iceberg 数据湖的 CDC 数据
简介: 数据湖的架构中,CDC 数据实时读写的方案和原理 本文由李劲松.胡争分享,社区志愿者杨伟海.李培殿整理.主要介绍在数据湖的架构中,CDC 数据实时读写的方案和原理.文章主要分为 4 个部分内容 ...
- Flink 1.14 新特性预览
简介: 一文了解 Flink 1.14 版本新特性及最新进展 本文由社区志愿者陈政羽整理,内容源自阿里巴巴技术专家宋辛童 (五藏) 在 8 月 7 日线上 Flink Meetup 分享的<F ...
- [Docker] 使 Volume 独立于容器运行时的方式 - 让容器引擎去处理
在单纯使用 run 命令运行一个容器时,通常会使用 -v 挂载的方式来实现宿主机数据卷映射到容器内. 使用命令: $ docker run --name mysql-con -v /my/custom ...