Forum Discussion
Anonymous
5 years agoNot applicable
Forbidden error on fetching dataset parameters
Please forgive me if I ask stupid quesions as I'm new to everything. Essentially I'd like to copy a Power BI report from one workspace to another and then modify the data source's parameters. The da...
Muesdo
2 years agoNew Member
Hi,
maybe I could still help someone
I was facing the same Error :
Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/datasets/$datasetId/Default.UpdateDatasources" -Method POST -Body $postBodyJson
Invoke-PowerBIRestMethod: One or more errors occurred. (Response status code does not indicate success: 403 (Forbidden).)
What I have done was:
- Verify the service principal's permissions: Ensure that the service principal has the required permissions to update data sources in the Power BI workspace. You can check the permissions by going to the Power BI Admin portal, selecting the "Tenant settings" tab, and then navigating to the "Developer settings" section. Verify that the service principal has the "Allow service principals to use Power BI APIs" setting enabled.
- Check the service principal's role in the workspace: Make sure that the service principal has the appropriate role assigned to the workspace where the dataset resides. The service principal should have at least the "Contributor" role or higher to be able to update data sources.
- Ensure that the service principal has the necessary API permissions: In the Azure Active Directory (AAD) portal, navigate to the service principal's "API permissions" section and verify that the necessary permissions are granted. The service principal should have the "Dataset.ReadWrite.All" and "Dataflow.ReadWrite.All" permissions.
But the Error was still the same. After this I found this PS Script: https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/PowerShell%20Scripts/Update-Connection-Details-for-Sql-Datasource.ps1
There I noticed they have build the URL different to mine. So I changed to:
$datasourePatchUrl = "groups/$workspaceId/datasets/$datasetId/Default.UpdateDatasources"
Invoke-PowerBIRestMethod -Method Post -Url $datasourePatchUrl -Body $postBodyJson
That works for me.
So to fetch information of the dataset associated with a workspace I think you could try this:
# get object for target workspace
$workspace = Get-PowerBIWorkspace -Name $workspaceName
# get object for new dataset
$dataset = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object Name -eq $datasetName
# get object for new SQL datasource
$datasource = Get-PowerBIDatasource -WorkspaceId $workspace.Id -DatasetId $dataset.Id
# parse REST to determine gateway Id and datasource Id
$workspaceId = $workspace.Id
$datasetId = $dataset.Id
$datasourceUrl = "groups/$workspaceId/datasets/$datasetId/datasources"
# execute REST call to determine gateway Id, datasource Id and current connection details
$datasourcesResult = Invoke-PowerBIRestMethod -Method Get -Url $datasourceUrl | ConvertFrom-Json
# parse REST URL used to patch datasource credentials
$datasource = $datasourcesResult.value[0]
$gatewayId = $datasource.gatewayId
$datasourceId = $datasource.datasourceId
$sqlDatabaseServerCurrent = $datasource.connectionDetails.server
$sqlDatabaseNameCurrent = $datasource.connectionDetails.database