Hi all!
A while ago I found this thread which contained the following code to connect to Synapse SQL serverless via REST API.
import requests
import json
# Azure app registration details
tenant_id = "<Your Tenant ID>"
client_id = "<Your Client ID>"
client_secret = "<Your Client Secret>"
# Get the Azure token
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://graph.microsoft.com/.default'
}
token_r = requests.post(token_url, data=token_data)
token = token_r.json().get("access_token")
# Synapse workspace details
workspace_name = "<Your Workspace Name>"
sql_endpoint = f"https://{workspace_name}.dev.azuresynapse.net"
# SQL Query
sql_query = "SELECT * FROM <Your Table>"
# REST API request to query data
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.post(f"{sql_endpoint}/query", headers=headers, json={"sql": sql_query})
# Check response
if response.status_code == 200:
print(response.json())
else:
print(f"Error: Unable to fetch data. Status code: {response.status_code}")
print(response.text)
I need a bit more information on the Service Principal, its permissions, and how to find the relevant connection details like the client ID and client secret. Can anyone shower me with wisdom on this?
Thanks!