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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
v-yangliu-msft
Community Support
Community Support

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 @v-yangliu-msft ,

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.