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

news/2024/9/20 0:13:30

问题描述

对于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 jsonconn = 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/

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/31953.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

【django学习-26】图片验证码

1.我们在登录的时候,经常看到输入用户名、密码之外。还需要输入验证码。这个验证码是怎么实现的呢?2.前端{% load static %} <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title>&…

mac指定虚拟网卡访问某个域名

通过 ifconfig 命令,一般会显示很多个虚拟网卡(冒号左边的就是网卡名)。 有些情况下,只能特定的网卡才能访问网站,所以需要指定网卡去访问。 指定网卡访问的命令(新增路由表) sudo route add -host 目标地址 -interface 网卡名删除指定网卡访问网站命令 sudo route del …

【django学习-25】登录及使用中间件进行登录校验

1.登录功能实现1.1:管理员表from django.db import modelsclass Admin(models.Model):""" 管理员 """username = models.CharField(verbose_name="用户名", max_length=32)password = models.CharField(verbose_name="密码"…

树链剖分[学习笔记]

树链剖分 壹. 树剖,就是树链剖分,将一棵树剖分成一堆链 (如说 \(\dots\) ) 本文主要介绍重链剖分。 树剖成链之后一段重链上的 \(dfs\) 序是连续的,那么我们就可以对 \(dfs\) 序使用一些数据结构(树状数组、线段树等)\(1\).一些变量及意义\(fa[x]\) \(x\) 的父节点 \(depth[…

[HDCTF 2023]YamiYami python中的另一种反序列化--yaml

今天做了到新颖的题,关于python中的yaml反序列化的题目,直接上题吧。发现第一个链接的参数是?url=XXXX,一眼利用点。嗯?直接出了flag,应该是非预期解。再看看有app.py,那就试试。发现app.*被过滤了,二次编码绕过试试。点击查看代码 @app.route(/) def index():session[p…

offsetExplorer3.0 如何连接加SASL认证的zookeeper、kafka

offsetExplorer3.0连接速度与查看topic、consumers查询速度显著提升。建议使用offsetExplorer3.0代替旧版offsetExplorer offsetExplorer3.0下载地址:https://www.kafkatool.com/download.html 配置方式如下:

msvc 获取c++类内存布局 /d1 reportAllClassLayout

visual studio 配置获取所有类内存布局 /d1 reportAllClassLayout 或者指定类 /d1 reportSingleClassLayoutXXXclass编译时输出: ps: https://www.openrce.org/articles/full_view/23【原文地址】https://blog.csdn.net/qq_29542611/article/details/79504396 VS2015 开发人员…

TypeError报错处理

哈喽,大家好,我是木头左!一、Python中的TypeError简介 这个错误通常表示在方法调用时,参数类型不正确,或者在对字符串进行格式化操作时,提供的变量与预期不符。 二、错误的源头:字符串格式化的奥秘 字符串格式化是Python中一个非常实用的功能,它允许根据一定的格式将变…