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

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
clearyj3
Frequent Visitor

API for GraphQL Saved Credentials

Hi,

 

I was wondering if anyone has been able to use a service principle to access graphql as they now support saved credentials? I am currently using a fabric notebook and have not been able to do so. Any insight would help, thanks. 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @clearyj3 

 

According to the following documentation, currently Service Principals do not support saved credentials. 

Connect applications to Fabric API for GraphQL - Microsoft Fabric | Microsoft Learn

Create and add data to an API for GraphQL - Microsoft Fabric | Microsoft Learn

vjingzhanmsft_0-1733465090918.png

 

Best Regards,
Jing
Community Support Team

View solution in original post

Anonymous
Not applicable

Hi @clearyj3 

 

Based on my testing, below code should work in a Fabric notebook. It uses Service principal with SSO. 

import requests
import json

# Client Application information
client_id = "app client id"
client_secret = "app client secret"
tenant_id = "your tenant id"

# Get token for service principal
url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"

payload = f'grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&scope=https%3A%2F%2Fapi.fabric.microsoft.com%2F.default'

headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers, data=payload)

if response.status_code == 200:
    # Parsing the JSON response
    json_response = response.json()
    # Extracting the token
    token = "Bearer " + json_response.get('access_token')
    print("Access token retrieved!")
else:
    print(f'Failed to get a response, status code: {response.status_code}')


# Use the token to make a POST request to the GraphQL API
workspace_id = "your workspace id"
graphql_api_id = "your graphql api item id"
api_endpoint = f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/graphqlapis/{graphql_api_id}/graphql"

graphql_query = json.dumps({
  "query": "{ customers { items { Id CustomerId Name } } }" # replace the query with your graphql query
})

query_headers = {
  'Content-Type': 'application/json',
  'Authorization': token
}

result = requests.request("POST", api_endpoint, headers=query_headers, data=graphql_query)

print(result.text)

This is my testing result:

vjingzhanmsft_0-1735542723548.png

 

To authenticate with a service principal, I create an application in Microsoft Entra ID and give it Power BI Service Item.Execute.All permission. Create a client secret and use it for retrieving the access token. No other setup is required in Azure portal. Then I add the service principal as a Contributor member of the workspace to ensure that it has access to the GraphQL API and the underlying data source. 

 

Hope this would be helpful. 

 

References:

Create and add data to an API for GraphQL - Microsoft Fabric | Microsoft Learn

Connect applications to Fabric API for GraphQL - Microsoft Fabric | Microsoft Learn

 

Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @clearyj3 

 

According to the following documentation, currently Service Principals do not support saved credentials. 

Connect applications to Fabric API for GraphQL - Microsoft Fabric | Microsoft Learn

Create and add data to an API for GraphQL - Microsoft Fabric | Microsoft Learn

vjingzhanmsft_0-1733465090918.png

 

Best Regards,
Jing
Community Support Team

Thank you! How exactly would the Service Principle work for SSO? I tried some things, but nothing has worked. 

 

Anonymous
Not applicable

Hi @clearyj3 

 

Based on my testing, below code should work in a Fabric notebook. It uses Service principal with SSO. 

import requests
import json

# Client Application information
client_id = "app client id"
client_secret = "app client secret"
tenant_id = "your tenant id"

# Get token for service principal
url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"

payload = f'grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&scope=https%3A%2F%2Fapi.fabric.microsoft.com%2F.default'

headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers, data=payload)

if response.status_code == 200:
    # Parsing the JSON response
    json_response = response.json()
    # Extracting the token
    token = "Bearer " + json_response.get('access_token')
    print("Access token retrieved!")
else:
    print(f'Failed to get a response, status code: {response.status_code}')


# Use the token to make a POST request to the GraphQL API
workspace_id = "your workspace id"
graphql_api_id = "your graphql api item id"
api_endpoint = f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/graphqlapis/{graphql_api_id}/graphql"

graphql_query = json.dumps({
  "query": "{ customers { items { Id CustomerId Name } } }" # replace the query with your graphql query
})

query_headers = {
  'Content-Type': 'application/json',
  'Authorization': token
}

result = requests.request("POST", api_endpoint, headers=query_headers, data=graphql_query)

print(result.text)

This is my testing result:

vjingzhanmsft_0-1735542723548.png

 

To authenticate with a service principal, I create an application in Microsoft Entra ID and give it Power BI Service Item.Execute.All permission. Create a client secret and use it for retrieving the access token. No other setup is required in Azure portal. Then I add the service principal as a Contributor member of the workspace to ensure that it has access to the GraphQL API and the underlying data source. 

 

Hope this would be helpful. 

 

References:

Create and add data to an API for GraphQL - Microsoft Fabric | Microsoft Learn

Connect applications to Fabric API for GraphQL - Microsoft Fabric | Microsoft Learn

 

Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.