Sub topic 10
Authenticating requests
This chapter assumes you have created the iris-endpoint serving endpoint from the previous chapter. It also assumes you know what an HTTP header is. Here you will see how a client proves who it is, and what the endpoint checks before it scores a row.
What the endpoint checks
Every request to /serving-endpoints/<name>/invocations must carry a bearer token in the Authorization header. Databricks does two checks:
- Authentication. Is the token valid and not expired? If not, you get
401. - Authorization. Does the identity behind the token have the CAN QUERY permission on the endpoint? If not, you get
403.
A wrong endpoint name returns 404. That is a different problem from either check above.
Two kinds of token
| Token | Identity | Lifetime | Typical use |
|---|---|---|---|
| Personal access token (PAT) | A user | Set when created, can be long | Local tests, notebooks |
| OAuth token for a service principal | A service principal | About one hour | Applications and production jobs |
A service principal is an identity for automation, not for a person. Production clients should use one. Its token expires quickly, so a leaked token does little harm. A PAT carries the full rights of its owner and is easy to forget about.
Using a personal access token
You create a PAT in your workspace user settings. Keep it in an environment variable, as the curl example in the topic plan does with $DATABRICKS_TOKEN. Never paste it into code or a notebook cell that you share.
Using OAuth for a service principal
Create a service principal, generate an OAuth secret for it, and note its client ID. Then request a token from the workspace:
curl -X POST "https://<workspace-host>/oidc/v1/token" \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-d "grant_type=client_credentials&scope=all-apis"The JSON response contains access_token and expires_in (in seconds). Send access_token exactly as you would a PAT.
Here is the full flow in Python. It fetches a token, then scores one row.
import os
import requests
host = os.environ["DATABRICKS_HOST"] # for example https://adb-123.4.azuredatabricks.net
token_response = requests.post(
f"{host}/oidc/v1/token",
auth=(os.environ["CLIENT_ID"], os.environ["CLIENT_SECRET"]),
data={"grant_type": "client_credentials", "scope": "all-apis"},
timeout=30,
)
token_response.raise_for_status()
token = token_response.json()["access_token"]
payload = {
"dataframe_split": {
"columns": ["sepal length (cm)", "sepal width (cm)",
"petal length (cm)", "petal width (cm)"],
"data": [[5.1, 3.5, 1.4, 0.2]],
}
}
response = requests.post(
f"{host}/serving-endpoints/iris-endpoint/invocations",
headers={"Authorization": f"Bearer {token}"},
json=payload,
timeout=60,
)
response.raise_for_status()
print(response.json())The service principal still needs CAN QUERY on iris-endpoint. You grant it in the endpoint's Permissions dialog or through the permissions API. Unity Catalog privileges such as EXECUTE on the model are not what the caller needs here. They matter when you create the endpoint.
Common errors
401 Unauthorized. The token is missing, mistyped, expired, or sent without theBearerprefix. Decode the problem by calling any simple workspace API with the same token.403 Forbidden. The token is valid, but its identity lacks CAN QUERY on the endpoint. Check the identity that owns the token. People often test with their own PAT and then deploy with a service principal that was never granted access.404 Not Found. The endpoint name or workspace host is wrong. Check both.- Token works in curl but not in code. A trailing newline in the environment variable is a frequent cause. Strip whitespace when you read secrets from files.
Exercise
A scheduled job calls your endpoint using a service principal's OAuth token. Requests return 403, but the same payload works with your own PAT. What is the most likely cause, and where do you fix it?
Show answer
The service principal lacks the CAN QUERY permission on the endpoint. Your own user has it, so your PAT passes. The token is valid, which rules out 401. Grant CAN QUERY to the service principal in the endpoint's Permissions settings.