本文将总结如何通过Azure Service Principal调用Power BI Rest API。
1:在Azure -> Microsoft Entra ID -> App registratios 新建一个registration,并记录相关信息
Display name : sp_test
Client ID : 2413efa3-b380-495c-97dd-c9bd18d2cd68
Tenant ID: d578c1bf-6d49-4e3f-8e2f-1da869f2e01f
Client secret : W7Z8Q~SSbSWrIWG9o-lK6exvzmOVe5o.KQinHc2Q
2: 在Azure -> Microsoft Entra ID -> Group -> 新建Security Group,并把第一步的sp_test添加进Members里
Group Name : G_PowerBI
Type : Security
3:进入Power BI -> Admin Portal -> Tenant settings 设置允许Service principals can use APIs,并Apply第二步的Group
4:通过Azure API 获取access token
POST : https://login.microsoftonline.com/d578c1bf-6d49-4e3f-8e2f-1da869f2e01f/oauth2/v2.0/token
Content-Type : application/x-www-form-urlencoded
Body:
grant_type : client_credentials
client_id : 2413efa3-b380-495c-97dd-c9bd18d2cd68
client_secret : W7Z8Q~SSbSWrIWG9o-lK6exvzmOVe5o.KQinHc2Q
scope : https://analysis.window.net/powerbi/api/.default
5: 把第二补的Group加入Power BI workspace里面,并赋予admin权限
官方设置文档请查看:https://learn.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal
6: 使用access token 请求Power BI Rest API,比如refresh dataset
POST https://api.powerbi.com/v1.0/myorg/datasets/cfafbeb1-8037-4d0c-896e-a46fb27ff229/refreshes
Headers Authorization Bearer access token
更多API请查看:https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/refresh-dataset