<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic API for GraphQL Saved Credentials in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4315660#M5508</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Dec 2024 17:50:11 GMT</pubDate>
    <dc:creator>clearyj3</dc:creator>
    <dc:date>2024-12-05T17:50:11Z</dc:date>
    <item>
      <title>API for GraphQL Saved Credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4315660#M5508</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 17:50:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4315660#M5508</guid>
      <dc:creator>clearyj3</dc:creator>
      <dc:date>2024-12-05T17:50:11Z</dc:date>
    </item>
    <item>
      <title>Re: API for GraphQL Saved Credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4316454#M5514</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/770219"&gt;@clearyj3&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;According to the following documentation, currently Service Principals do not support saved credentials.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-engineering/connect-apps-api-graphql" target="_blank" rel="noopener"&gt;Connect applications to Fabric API for GraphQL - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-engineering/get-started-api-graphql" target="_blank" rel="noopener"&gt;Create and add data to an API for GraphQL - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vjingzhanmsft_0-1733465090918.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1210966iC354129B730182BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vjingzhanmsft_0-1733465090918.png" alt="vjingzhanmsft_0-1733465090918.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Jing&lt;BR /&gt;Community Support Team&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 06:05:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4316454#M5514</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-12-06T06:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: API for GraphQL Saved Credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4317580#M5525</link>
      <description>&lt;P&gt;Thank you! How exactly would the Service Principle work for SSO? I tried some things, but nothing has worked.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 20:45:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4317580#M5525</guid>
      <dc:creator>clearyj3</dc:creator>
      <dc:date>2024-12-06T20:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: API for GraphQL Saved Credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4345971#M5815</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/770219"&gt;@clearyj3&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on my testing, below code should work in a Fabric notebook. It uses Service principal with SSO.&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;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&amp;amp;client_id={client_id}&amp;amp;client_secret={client_secret}&amp;amp;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)&lt;/LI-CODE&gt;
&lt;P&gt;This is my testing result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vjingzhanmsft_0-1735542723548.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1220577i36539FED311A7413/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vjingzhanmsft_0-1735542723548.png" alt="vjingzhanmsft_0-1735542723548.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To authenticate with a service principal, I create an application in Microsoft Entra ID and give it Power BI Service&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;Item.Execute.All&lt;/STRONG&gt; permission. Create a client secret and use it for retrieving the access token.&amp;nbsp;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.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this would be helpful.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;References:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-engineering/get-started-api-graphql" target="_blank" rel="noopener"&gt;Create and add data to an API for GraphQL - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-engineering/connect-apps-api-graphql#use-a-service-principal" target="_blank" rel="noopener"&gt;Connect applications to Fabric API for GraphQL - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Jing&lt;BR /&gt;&lt;EM&gt;If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2024 07:30:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/API-for-GraphQL-Saved-Credentials/m-p/4345971#M5815</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-12-30T07:30:42Z</dc:date>
    </item>
  </channel>
</rss>

