【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码

简介: 【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。

 

第三步:在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/

 

相关文章
|
4天前
|
API 网络安全 网络架构
【Azure APIM】解答REST API实现"禁用自签名证书的证书链验证"中的backends参数值从那里取值的问题?
本文介绍APIM服务调用后端API时因自签名证书导致500错误的解决方案。通过REST API禁用证书链验证,关键在于获取正确的backendId(即APIM中配置的Backend名称),并调用PATCH接口设置validateCertificateChain为false,从而解决SSL/TLS信任问题。
|
8天前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
194 100
|
8天前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
171 95
|
16天前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
171 104
|
16天前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
293 99
|
8天前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
127 88
|
14天前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。
|
28天前
|
数据可视化 测试技术 API
从接口性能到稳定性:这些API调试工具,让你的开发过程事半功倍
在软件开发中,接口调试与测试对接口性能、稳定性、准确性及团队协作至关重要。随着开发节奏加快,传统方式已难满足需求,专业API工具成为首选。本文介绍了Apifox、Postman、YApi、SoapUI、JMeter、Swagger等主流工具,对比其功能与适用场景,并推荐Apifox作为集成度高、支持中文、可视化强的一体化解决方案,助力提升API开发与测试效率。
|
2月前
|
JSON 算法 安全
淘宝商品详情API接口系列,json数据返回
淘宝开放平台提供了多种API接口用于获取商品详情信息,主要通过 淘宝开放平台(Taobao Open Platform, TOP) 的 taobao.tbk.item.info.get(淘宝客商品详情)或 taobao.item.get(标准商品API)等接口实现。以下是关键信息及JSON返回示例:
|
7天前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。

推荐镜像

更多