如何使用Python对Azure进行原始REST调用 [英] How to make Raw REST Call for Azure using Python
问题描述
我正在尝试使用python进行REST调用以进行天蓝色处理,在python中使用ADAL创建了访问令牌.但是,收到名为提供的授权标头的错误格式无效"的错误消息.这是该代码:
I am trying to make REST Call for azure using python, Have created Access token using ADAL in python. But getting Error called "provided Authorization header is in invalid format." Here is the code for that:
import adal
import requests
token_response = adal.acquire_token_with_username_password(
'https://login.windows.net/abcd.onmicrosoft.com',
'user-name',
'password'
)
access_token = token_response.get('accessToken')
url = 'https://management.azure.com/subscriptions/{subscription- id}/providers/Microsoft.Network/virtualnetworks?api-version=2015-06-15'
headers = {'Content-Type': 'application/json',
'Authorization': access_token}
response = requests.get(url=url,headers = headers)
print(response.status_code)
print(response.text)
任何人都可以告诉我访问令牌的外观吗?这是在python中为REST生成令牌的正确方法吗?我在上面的代码中引用了此链接: https://msdn.microsoft.com/en-us/library/azure/mt163557.aspx
Can anyone tell me how the access-token should look like? And is this the correct way to generate token for REST in python? I am reffering this link for above code: https://msdn.microsoft.com/en-us/library/azure/mt163557.aspx
推荐答案
正如@GauravMantri所说,标头 Authorization
的值的格式为 Bearer< access-token>
,您可以参考 调用文档"Resource Manager REST API"的ARM REST API
.
As @GauravMantri said, the format of the value of the header Authorization
is Bearer <access-token>
that you can refer to the section Calling ARM REST APIs
of the doc "Resource Manager REST APIs".
例如在上面的部分中.
获取/subscriptions/SUBSCRIPTION_ID/resourcegroups?api-version = 2015-01-01 HTTP/1.1
主持人:management.azure.com
授权:承载YOUR_ACCESS_TOKEN
内容类型:application/json
GET /subscriptions/SUBSCRIPTION_ID/resourcegroups?api-version=2015-01-01 HTTP/1.1
Host: management.azure.com
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json
这篇关于如何使用Python对Azure进行原始REST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!