cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI API for dataset refresh always unauthorized returned

Hi All,

We are currently having issues using the REST API to access Power BI using a service principal.

We have granted the app tenant.readwrite.all permissions with type = application access not delegated . 

We have added the app in a security group that has been granted access to API in the PowerBI Admin portal

We have the app as an admin in the workgrp we are trying to access. 

With all this still get Unauthorized error when calling Get-PowerBIWorkspace after a successful login from Powershell.

Here is teh script we are trying to run

 

##The  workspace name where datasets to refresh are located

$workspace = "XXXX_DEV"    

 

$secret = 'XXXXXXXXXXXXX'| ConvertTo-SecureString -AsPlainText -Force

$azureAppId =  'XXXXXXXXX-XXXX-XXXX-XXXX-fac8abedb3e2'

$azureAppCred = New-Object -TypeName System.Management.Automation.PSCredential($azureAppId, $secret)

$tenantId = 'XXXXXXXXX-XXXX-XXXX-XXXX-61e00287adfe'

Connect-PowerBIServiceAccount   -ServicePrincipal  -TenantId $tenantId -Credential $azureAppCred

 

 

*****************this next line gives “Get-PowerBIWorkspace : Operation returned an invalid status code 'Unauthorized'”*********************

##Get the  for the workspace information

$devworkspace = Get-PowerBIWorkspace -Name $workspace   

 

##Get datasets that exist within the requested workspace

$DataSetIDs = Get-PowerBIDataset -WorkspaceId $devworkspace.Id 

 

##Set the body of the rest API Invoke  for post method the body paramter is required

$RestMethodBody = @{"privacyLevel" = "Organizational"} | ConvertTo-Json

##Loop through each dataset check the name and refresh if desired.

ForEach ($Id in $DataSetIDs.ID)

{

 $currentDataset = Get-PowerBIDataset -Id $Id  -WorkspaceId $devworkspace.Id

 Write-Host $currentDataset.Name

    If($currentDataset.Name -eq 'PortfolioPosition' -Or $currentDataset.Name -eq 'PortfolioTransaction')  ##List reports to be refreshed here this could be read from a file if needed at a later date

        {$DSURL = 'groups/' + $devworkspace.Id +'/datasets/' +$Id + '/refreshes'  ##rest API end point URL for dataset refresh

 

        Invoke-PowerBIRestMethod -Url $DSURL -Method Post -Body $RestMethodBody

            }

}

 

Disconnect-PowerBIServiceAccount

Status: New
Comments
Community Support

@ausgod 

 

You may check Considerations and limitations section.