Forum Discussion
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.
- Anonymous1 year ago
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
Best Regards,
Jing
Community Support Team - Anonymous1 year ago
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:
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!
3 Replies
- AnonymousNot 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
Best Regards,
Jing
Community Support Team- clearyj3Frequent Visitor
Thank you! How exactly would the Service Principle work for SSO? I tried some things, but nothing has worked.
- AnonymousNot 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:
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!