我有下面的Python代码试图获取
Sales by Product/Service Detail
报告数据。
在文档下:https://developer.intuit.com/app/developer/qbo/docs/api/accounting/report-entities/SalesByProduct
看起来终点将是
[ItemSales]
,尽管 API 文档的终点是 [SalesByProduct]
,这令人困惑。
如何更改下面的代码才能提取所有默认指标:
我附上了运行
Sales by Product/Service Detail
报告时报告在 QuickBooks UI 下的外观的屏幕截图。
此外,我希望能够提取所有数据,目前我只获得 96 个对象,但当我通过 UI 运行报告时,我获得 2315 行。报告 API 中有分页机制吗?如果是这样,我该如何实施?
注意:当我在 UI 下运行报告时,我没有使用任何分组值来查看项目行级别的数据,这是否意味着我不应该使用 [summarize_column_by] 属性
def fetch_sales_by_product(access_token):
url = f'https://quickbooks.api.intuit.com/v3/company/11111111111/reports/ItemSales'
headers = {
'Authorization': f'Bearer {access_token}',
'Accept': 'application/json'
}
params = {
'start_date': '2021-01-01',
'end_date': '2024-04-01',
'summarize_column_by': 'ProductsAndServices'
}
all_data = []
counter = 1
while True:
response = requests.get(url, headers=headers, params=params)
print(f"{response.status_code=}")
if response.status_code != 200:
raise Exception(f"Failed to fetch report: {response.text}" )
data = response.json()
all_data.extend(data['Rows']['Row'])
counter += 1
return all_data
修改提供的 Python 代码以提取所有默认指标(产品/服务、日期、交易类型、数量、客户名称、备忘录/描述、数量、费率、金额、余额)并处理分页以检索所有数据QuickBooks 中的 Sales by Product/Service Detail 报告,您需要进行以下调整:
这是修改后的Python代码:
import requests
def fetch_sales_by_product(access_token): url = 'https://quickbooks.api.intuit.com/v3/company/11111111111/reports/SalesByProduct' 标题= { '授权': f'承载{access_token}', '接受': '应用程序/json' }
params = {
'start_date': '2021-01-01',
'end_date': '2024-04-01'
}
all_data = []
while True:
response = requests.get(url, headers=headers, params=params)
print(f"Status code: {response.status_code}")
if response.status_code != 200:
raise Exception(f"Failed to fetch report: {response.text}")
data = response.json()
all_data.extend(data['Rows']['Row'])
# Check for pagination
if 'next_page_url' in data:
url = data['next_page_url']
# Extracting page number from the next_page_url
page_number = int(url.split('=')[-1])
# Update params with the page number
params['page'] = page_number
else:
break
return all_data
此修改后的代码现在应该通过处理分页而不汇总数据,正确从 QuickBooks 中的按产品/服务详细信息销售报告中获取所有数据。此外,它还确保 API 请求期间任何潜在问题的错误处理。