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! Learn more

Reply
Uspace87
Resolver III
Resolver III

Rest API

Hi all,

I need all with a Rest Api Post call I am trying to make to bind one dataset to a specific connector. I don't know what I am doing wrong but it does not work.

I have add the service principal as user to the gateway and to the connector and it ass all the permission needed as per Microsoft documentation ( DatasetReadWriteall etc). 

I will paste the python code I am using: 

 


import requests
import msal
import json

# Azure AD and app registration details
client_id = "xxx"
tenant_id = "xxx"
client_secret = "xxx"
authority = f"https://login.microsoftonline.com/{tenant_id}"
scope = ["https://analysis.windows.net/powerbi/api/.default"]  # Power BI scope for client credentials

# Create a ConfidentialClientApplication instance
app = msal.ConfidentialClientApplication(
    client_id,
    authority=authority,
    client_credential=client_secret,
)

# Acquire token using the client credentials
token_response = app.acquire_token_for_client(scopes=scope)

# Check if the token acquisition was successful
if "access_token" in token_response:
    access_token = token_response["access_token"]
    print("Access Token:", access_token)
else:
    print("Failed to acquire token:", token_response.get("error_description"))

# Set the API endpoint URL
groupId = "xxx"
dataset_id = "xxx"

url = f"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{dataset_id}/Default.BindToGateway"

# Define the payload for the API request
payload = {

     "gatewayObjectId": "xxx"
     ,"datasourceObjectIds": [ "xxx"]
     
}

# Set the request headers
headers = {
    "Authorization": f"Bearer {token_response}",
    "Content-Type": "application/json"
}

# Make the POST request
response = requests.post(url, data=payload, headers=headers)

# Check the response
if response.status_code == 200:
    print("Dataset successfully bound to gateway.")
else:
    print(f"Failed to bind dataset to gateway: {response.status_code} - {response.text}")
5 REPLIES 5
Anonymous
Not applicable

Thanks for the reply from lbendlin , please allow me to provide another insight: 
Hi  @Uspace87 ,

 

According to the error message description: you can try to check admin portal -- search service principal settings -- is AAD account open for this user?

  1. Go to the Power BI Admin portal and then select Tenant settings.
  2. Scroll to Developer settings and then expand Allow service principals to use Power BI APIs.
  3. Select Enabled.

Refer to:

Automate Power BI Premium workspace and semantic model tasks with service principals - Power BI | Mi...

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @Anonymous ,

thanks for you reply. 
That option is Enabled.  We heavly usi Rest Api in our company. I am not sure if the proble is related to the pyhton code or how I pass the Payload with the information for the gateawy and datasource to bind the dataset to. The call is working on the power bi website with my credentials.

lbendlin
Super User
Super User

What is the error message?  Examine your token with jwt.io to make sure it has the required scope.

hi @lbendlin  thanks for the reply.  I get 403 error. The token get returned but I get an error. the service principall has all the required permissions in azure.

Can you try the same in Powershell?

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.