Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
bmtolleson
Regular Visitor

Gateway Binding using the API

I get an Error 400 (Bad Request) using the code below.  I have looked at it for hours.  Does anyone see anything wrong?  Any suggestions?

 

#Login to Power BI using PowerBIPS App registration.
Write-Host "Start Login"
$ApplicationId = "abc123"
$SecurePassword = "password" | ConvertTo-SecureString -AsPlainText -force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecurePassword
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId "ten123"
Write-Host "Login Success"

Write-Host "Get Workspace info for workspace: AC Common_TST"
$workspaceName = "AC Common_TST"
$datasetName = "DateDim"
$workspace = Get-PowerBIWorkspace -Name $workspaceName
$workspaceObject = ( Get-PowerBIWorkspace -Name "AC Common_TST" )
$groupid=$workspaceObject.id
$newGatewayId = "gatewayid" # the ID of PRDGateway1
$dataset = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object Name -eq $datasetName
$datasetid = $dataset.id
$workspaceId = $workspace.Id

Write-Host "GroupID is "$workspace.id
Write-Host "Dataset ID is "$dataset.id

# POST body
$postParams = @'
{
"gatewayObjectId": "gatewayid",
"datasourceObjectIds": [
"dsid"
]
}
'@
$jsonPostBody = $postParams | ConvertTo-JSON

$uri = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceid/datasets/$datasetId/Default.BindToGateway"

Invoke-PowerBIRestMethod -URL $uri -Method POST -Body $jsonPostBody
Resolve-PowerBIError -Last

2 REPLIES 2
Anonymous
Not applicable

Hi @bmtolleson ,

The following ones are the threads which have the similar problem as yours, please check their solution. Hope they can also help solve your problem.

BindToGateway Powershell REST API POST failing

I have found the reasons why this wasnt working so thought I would list them here for future users. To get this to work there are a few things to be aware of:

 

1. If the datasource is not assigned to a gateway you need to not include the datasource ID values (you wont actually be able to get these ID values if it is unassigned). I assume the ability to specify the data source id values is only relevent when the dataset is already assigned to a gateway, but I dont know if you can use two separate gateways for one dataset with two datasources.

 

2. Service principals cant be set to administrators of gateways, so you cant use them

 

3. Users must be set as administrators of gateways

 

4. The user must be assigned as a user of the data source defined on the gateway (different from being a gateway admin)


Getting Token for API Dataflow Gateway Binding

Best Regards

I tried the suggestions in that post, but no luck.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.