Sub topic 3
Authenticating with OAuth or a personal access token
This chapter assumes you have installed the Databricks CLI, can set environment variables in your shell, and know that every Databricks API call must identify who is making it. The next chapter covers profiles in more detail.
Two ways to prove identity
Databricks accepts two kinds of credentials for CLI and REST calls.
- OAuth issues short-lived access tokens, which the CLI refreshes for you. A person uses OAuth user-to-machine (U2M), which opens a browser login. A program uses OAuth machine-to-machine (M2M), which authenticates a service principal with a client ID and client secret.
- A personal access token (PAT) is a long-lived string you create in the workspace. It is sent as a bearer token in the
Authorizationheader.
OAuth is the better default because its tokens expire within an hour and can be revoked centrally. A PAT is simpler, but anyone who copies it can act as you until it expires or you revoke it.
Logging in with OAuth (U2M)
Run the login command with your workspace URL. The CLI opens a browser, you sign in, and the CLI saves a profile in ~/.databrickscfg.
databricks auth login --host https://<workspace-host>
databricks current-user me
databricks auth profilescurrent-user me confirms which user the CLI is acting as. auth profiles lists the saved profiles and shows whether each one is valid.
Authenticating a script (M2M)
An unattended job cannot open a browser. Create a service principal, generate an OAuth secret for it, and give the script three environment variables.
export DATABRICKS_HOST=https://<workspace-host>
export DATABRICKS_CLIENT_ID=<service-principal-application-id>
export DATABRICKS_CLIENT_SECRET=<oauth-secret>
databricks current-user meThe CLI exchanges the client ID and secret for an access token and renews it when it expires.
Using a personal access token
Create a PAT in the workspace under your user settings, in the Developer section, then export it.
export DATABRICKS_HOST=https://<workspace-host>
export DATABRICKS_TOKEN=<personal-access-token>
databricks current-user meWorkspace admins can disable PATs or cap their lifetime, so this option may not be available to you.
Comparison
| Method | Lifetime | Best for | Main risk |
|---|---|---|---|
| OAuth U2M | Short, auto-refreshed | Interactive work | Tied to a person's account |
| OAuth M2M | Short, auto-refreshed | Scripts and CI/CD | Leaked client secret |
| PAT | Days to months | Quick tests, tools without OAuth | Long-lived and easily copied |
Common errors
- 401 Unauthorized or "invalid access token": the token expired, was revoked, or belongs to a different workspace than
DATABRICKS_HOST. - Wrong identity: environment variables take precedence over a saved profile, so a stale
DATABRICKS_TOKENcan override your OAuth login. Rundatabricks current-user meto see who you are. - Browser login fails on a remote machine: use M2M or a PAT instead.
Exercise
A script exports DATABRICKS_TOKEN and also has an OAuth profile from databricks auth login. databricks current-user me returns a 401 error. What is the most likely cause, and how do you check it?
Show answer
The expired or revoked DATABRICKSTOKEN is taking precedence over the working OAuth profile. Run env | grep DATABRICKS to see the variables, then `unset DATABRICKSTOKEN and run databricks current-user me` again to confirm the profile works.