Python Azure REST API使用:puid为null/空
我正在尝试使用REST API自动下载Azure消费信息。Python Azure REST API使用:puid为null/空,python,azure,rest,python-requests,azure-rest-api,Python,Azure,Rest,Python Requests,Azure Rest Api,我正在尝试使用REST API自动下载Azure消费信息。 我修改了返回订阅列表的示例 我更改了url并添加了两个附加参数 import adal import requests authentication_endpoint = 'https://login.microsoftonline.com/' resource = 'https://management.core.windows.net/' application_id = '4c681786-980d-44cb-89f9-123
我修改了返回订阅列表的示例
我更改了url并添加了两个附加参数
import adal
import requests
authentication_endpoint = 'https://login.microsoftonline.com/'
resource = 'https://management.core.windows.net/'
application_id = '4c681786-980d-44cb-89f9-123456789012'
application_secret = 'xxxxxxxxxxxxxxxxxxxxxxx'
tenant_id = 'xxxxxxxxx-xxxx-xxxx-xxxx-123456789012'
enrollmentNumber = 'XXXXXXXX'
# get an Azure access token using the adal library
context = adal.AuthenticationContext(authentication_endpoint + tenant_id)
token_response = context.acquire_token_with_client_credentials(resource, application_id, application_secret)
access_token = token_response.get('accessToken')
# endpoint = 'https://management.azure.com/subscriptions/?api-version=2015-01-01'
scope = '/providers/Microsoft.Billing/enrollmentAccounts/' + enrollmentNumber
endpoint = 'https://management.azure.com' + scope + '/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01'
headers = {"Authorization": 'Bearer ' + access_token}
json_output = requests.get(endpoint,headers=headers).json()
print(json_output)
使用消费信息代替json,我得到一个错误:
Puid is null/empty. Puid must be present in the header for user to get authorized.
使用相同的令牌,我可以获得所有订阅的列表。我已经查看了您的请求,并根据调查结果,请注意,PUID是特定于用户对象的属性,Azure AD中的服务主体没有PUID。然而,计费平台很可能需要有权读取计费数据的用户的PUID 需要以下标题
x-ms-client-tenant-id :
x-ms-client-object-id :
x-ms-client-principal-id:
其中
我建议您尝试使用用户凭据使用管理api
或者,您必须在azure AAD租户中使用本机客户端应用程序来访问计费api,有关更多信息,请查看以下链接:
https://github.com/Azure-Samples/billing-dotnet-usage-api#step-1-configure-a-native-client-application-in-your-aad-tenant
希望有帮助。我是一个允许查看这些数据的用户。例如,我可以手动下载成本管理+计费-使用+费用,如“使用详细信息版本2”。我也可以在企业协议中手动下载使用信息。我认为通过使用令牌(将其添加到请求中),我将被识别为具有访问权限的人。实际上,上面的代码使用我在AAD和secret上注册的应用程序对应用程序进行身份验证。