I'm trying to use this API call: https://learn.microsoft.com/en-us/rest/api/power-bi/admin/groups-add-user-as-admin.
I'm authenticating with a Service Principal which has tenant.ReadWrite.All and Fabric Administrator role (previously Power BI Service Administrator).
With the token generated with the Service Principal, I'm able to run other Admin APIs like https://learn.microsoft.com/en-us/rest/api/power-bi/admin/groups-get-group-as-admin.
However, when running Add user as admin, I'm getting 401 unauthorized.
I'm runnning it with Python
group_id = '<workspace_id>'
aad_group_object_id = '<servicePrincipal_cliend_id>'
new_permission = 'Contributor'
url = f'https://api.powerbi.com/v1.0/myorg/admin/groups/{group_id}/users'
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
data = {
'principalType': 'App',
'identifier': aad_group_object_id,
'groupUserAccessRight': new_permission
}
response = requests.post(url, headers=headers, json=data)
I cannot see how there's an authorization issue, any ideas?
When I ran same code by passing service principal
clientIDfor identity parameter and token generated with client credentials flow, I too got same error as below:Response:
To resolve the error, make sure to pass service principal
objectIDfor identity parameter that can be found in Enterprise applications tab with same name:In my case, I generated token using username password flow by granting
Tenant.ReadWrite.AllDelegated permission via Postman like this:Response:
When I ran same code by passing above token and service principal
objectIDfor identity parameter, I got response successfully like this:Response:
To confirm that, I checked the same in Power Bi portal where service principal added with
Contributorrole like this: