Skip to content

Instantly share code, notes, and snippets.

@JamesDLD
Last active July 27, 2023 08:27
Show Gist options
  • Save JamesDLD/3080e0c74271998f6cc5e8fbacd0d968 to your computer and use it in GitHub Desktop.
Save JamesDLD/3080e0c74271998f6cc5e8fbacd0d968 to your computer and use it in GitHub Desktop.
Databricks authentication through API
# Variables
$DBXressource = "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d" # CF. AzureDatabricks AzureAD application. Seems unique for all Azure AD tenant.
$servicePrincipalName = "dbx-adm-spn1" # Service Principal that has the Owner privilege on the Databricks resource "dld-corp-mvp-dbx"
$servicePrincipalSecret = "SecureSecret" # The Service Principal Secret
$SubscriptionId="xxxx-xxx-xxxx-xxxx" # The Subscription id where the Databricks ressource belongs to
$ResourceGroupName = "dld-corp-mvp-dataplatform" # The Rresource Group name where the Databricks ressource belongs to
$WorkspaceName = "dld-corp-mvp-dbx" # The name of the Databricks ressource
$Resource = "https://management.core.windows.net/"
# Connect to Azure
Connect-AzAccount
$TenantId=(Get-AzContext).Tenant.Id
$RequestAccessTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
# Get Databricks
$dbxurl = (Get-AzResource -Name $WorkspaceName -ResourceGroupName $ResourceGroupName -ExpandProperties).Properties.workspaceUrl
$uriroot = "https://$dbxurl/api"
# Get the Service Principal that has been granted the "Owner" privilege on Databricks
$servicePrincipal = Get-AzADServicePrincipal -DisplayName $servicePrincipalName
$servicePrincipleNameId = $servicePrincipal.AppId
# Get AzureDatabricks app token
$body = "grant_type=client_credentials&client_id=$servicePrincipleNameId&client_secret=$servicePrincipalSecret&resource=$DBXressource"
$Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'
$apiKey = $Token.access_token
# Get Azure Management token
$bodyManagement = "grant_type=client_credentials&client_id=$servicePrincipleNameId&client_secret=$servicePrincipalSecret&resource=$Resource"
$Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $bodyManagement -ContentType 'application/x-www-form-urlencoded'
$apiKeyManagement = $Token.access_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment