调用Azure Price Sheet API返回404错误的排查求助
Azure Consumption Price Sheet API 404错误排查
我需要获取Azure订阅中资源的价目表(含折扣信息),按照API文档说明,通过计费周期方法调用接口返回价格数据。认证环节正常,但调用后始终返回404错误。
已尝试的排查操作:
- 更换API端点
- 检查订阅激活状态
- 配置API访问权限并为应用服务分配对应角色
- 核对租户ID、客户端ID等参数
相关代码:
import requests # Define your Azure AD and API parameters tenant_id = 'xxx' client_id = 'yyy' client_secret = 'zzz' subscriptionId = 'www' resource = 'https://management.azure.com/' billingPeriodName = "202407" # OAuth2 endpoint for token retrieval token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token' # Request headers and body for token retrieval token_data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret, 'scope': 'https://management.azure.com/.default' } # Get the access token response = requests.post(token_url, data=token_data) response.raise_for_status() access_token = response.json().get('access_token') # Endpoint to retrieve the current retail prices retail_prices_url = f'https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Billing/billingPeriods/{billingPeriodName}/providers/Microsoft.Consumption/pricesheets/default?api-version=2023-05-01' # Request headers for the API call headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' } # Get the current retail prices response = requests.get(retail_prices_url, headers=headers) response.raise_for_status() retail_prices_data = response.json() response.raise_for_status() # Raise an error for bad status codes print(token_data) print(pricesheet_data)
错误分析与修正方案
1. API端点路径错误
当前端点错误嵌套了Microsoft.Billing/billingPeriods/{billingPeriodName}层级,正确的按计费周期查询价格表的方式是通过**查询参数$filter**指定周期,而非嵌入路径。修正后的端点格式:
https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Consumption/pricesheets/default?api-version=2023-05-01&$filter=billingPeriodName eq '{billingPeriodName}'
2. 变量名错误
代码最后打印的pricesheet_data未定义,实际获取的变量是retail_prices_data,会导致运行时抛出NameError。
3. 冗余异常抛出
response.raise_for_status()在获取响应后重复调用,只需一次即可触发错误状态码的异常。
修正后的代码
import requests # Define your Azure AD and API parameters tenant_id = 'xxx' client_id = 'yyy' client_secret = 'zzz' subscriptionId = 'www' billingPeriodName = "202407" # OAuth2 endpoint for token retrieval token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token' # Request headers and body for token retrieval token_data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret, 'scope': 'https://management.azure.com/.default' } # Get the access token response = requests.post(token_url, data=token_data) response.raise_for_status() access_token = response.json().get('access_token') # 修正后的API端点:通过$filter指定计费周期 retail_prices_url = f'https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Consumption/pricesheets/default?api-version=2023-05-01&$filter=billingPeriodName eq \'{billingPeriodName}\'' # Request headers for the API call headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' } # Get the current retail prices response = requests.get(retail_prices_url, headers=headers) response.raise_for_status() retail_prices_data = response.json() print(token_data) print(retail_prices_data)
额外注意事项
- 确认应用服务已在订阅级别分配
Cost Management Reader或Reader角色 - 验证
billingPeriodName = "202407"是否为订阅实际存在的计费周期(可通过计费周期API确认)
内容的提问来源于stack exchange,提问作者guzassis
相关产品推荐
相关产品推荐

