Power BI数据集数据导入问题:调用REST API返回404错误
Power BI数据集导出JSON/CSV数据及404错误修复
能否从Power BI数据集导出JSON/CSV格式数据?
可以通过Power BI REST API导出数据集行数据为JSON格式,后续可自行转换为CSV等格式。
404错误的原因及修复
你遇到的404错误是API端点路径格式错误导致的,具体问题及修正方案如下:
- 若数据集在指定工作区(组)中,正确端点必须包含工作区ID:
你的代码漏写了https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/tables/{table_name}/rows{group_id}占位符,直接写成groups/datasets,导致路径无效。 - 若数据集属于「我的工作区」(未加入其他工作区),则不需要
groups/{group_id}部分,正确路径为:https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/tables/{table_name}/rows - 需替换代码中的
dataset_id、Table_name(工作区场景下还需group_id)为实际的ID和表名。
修正后的Python代码
场景1:数据集在指定工作区
import requests import pandas as pd # 替换为实际参数 group_id = "你的工作区ID" dataset_id = "你的数据集ID" table_name = "你的表名" access_token = result['access_token'] endpoint = f'https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/tables/{table_name}/rows' headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } response = requests.get(endpoint, headers=headers) print(f"响应状态码: {response.status_code}") if response.status_code == 200: # 获取JSON格式数据 json_data = response.json() print(json_data) # 转换为CSV格式(可选) df = pd.DataFrame(json_data['value']) df.to_csv('powerbi_data.csv', index=False, encoding='utf-8-sig') else: print(f"获取数据失败: {response.text}")
场景2:数据集在「我的工作区」
import requests import pandas as pd # 替换为实际参数 dataset_id = "你的数据集ID" table_name = "你的表名" access_token = result['access_token'] endpoint = f'https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/tables/{table_name}/rows' headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } response = requests.get(endpoint, headers=headers) print(f"响应状态码: {response.status_code}") if response.status_code == 200: # 获取JSON格式数据 json_data = response.json() print(json_data) # 转换为CSV格式(可选) df = pd.DataFrame(json_data['value']) df.to_csv('powerbi_data.csv', index=False, encoding='utf-8-sig') else: print(f"获取数据失败: {response.text}")
额外说明
- 确保Access Token拥有Dataset.Read.All或对应数据集的读取权限,否则会返回权限错误。
- API返回的JSON数据中,实际行数据存放在
value字段下,转换CSV时需提取该字段。
内容的提问来源于stack exchange,提问作者Dhayf OTHMEN
相关产品推荐
相关产品推荐

