nayan_wylde
Esteemed Contributor II

@noorbasha534  Here is a sample python code I use for getting oauth token from Azure Active Directory and then pass the token in databricks API. Prerequisite is the SPN needs to be a admin in the workspace.

import requests

# Azure AD credentials
tenant_id = 'your-tenant-id'
client_id = 'your-client-id'
client_secret = 'your-client-secret'

# Databricks workspace URL
databricks_instance = 'https://<your-databricks-instance>.azuredatabricks.net'

# Step 1: Get OAuth token from Azure AD
def get_aad_token(tenant_id, client_id, client_secret):
    url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    response = requests.post(url, data=payload)
    response.raise_for_status()
    return response.json()['access_token']

# Step 2: Use token to call Databricks API
def call_databricks_api(token, endpoint='/api/2.0/clusters/list'):
    headers = {
        'Authorization': f'Bearer {token}'
    }
    url = f"{databricks_instance}{endpoint}"
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# Example usage
token = get_aad_token(tenant_id, client_id, client_secret)
result = call_databricks_api(token)
print(result)