【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/

 

相关文章
|
10月前
|
API 网络安全 网络架构
【Azure APIM】解答REST API实现"禁用自签名证书的证书链验证"中的backends参数值从那里取值的问题?
本文介绍APIM服务调用后端API时因自签名证书导致500错误的解决方案。通过REST API禁用证书链验证,关键在于获取正确的backendId(即APIM中配置的Backend名称),并调用PATCH接口设置validateCertificateChain为false,从而解决SSL/TLS信任问题。
318 7
|
10月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
10月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
10月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
407 100
|
10月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
628 95
|
11月前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
432 104
|
11月前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
726 99
|
10月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
555 88
|
10月前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。
1692 68
|
11月前
|
设计模式 人工智能 API
AI智能体开发实战:17种核心架构模式详解与Python代码实现
本文系统解析17种智能体架构设计模式,涵盖多智能体协作、思维树、反思优化与工具调用等核心范式,结合LangChain与LangGraph实现代码工作流,并通过真实案例验证效果,助力构建高效AI系统。
1120 7

推荐镜像

更多