I am trying to generate an OAuth token for my Azure Databricks workspace to access a model serving API in production. The code Iโm using generates a token successfully, but I keep receiving a 401 error with the message "Missing authorization details for accessing model serving endpoints" when I try to use it.
Here's the code I'm using:
import requests
# Azure Databricks and Managed Service Principal Details
client_id = "XXXX" # Application ID
client_secret = "XXXXX" # Client Secret
workspace_url = "XXXXXX" # Databricks workspace URL
# Databricks-specific endpoint URL for token generation
token_endpoint_url = f"{workspace_url}/oidc/v1/token"
# Function to get OAuth access token with all-apis scope
def get_workspace_oauth_token():
response = requests.post(
token_endpoint_url,
auth=(client_id, client_secret),
data={
"grant_type": "client_credentials",
"scope": "all-apis"
}
)
response.raise_for_status() # Check for request errors
return response.json(), response.json().get("access_token")
# Generate the access token
response, access_token = get_workspace_oauth_token()
print("OAuth token generated successfully:", access_token)
The OAuth token seems to be generated without any issues. Iโve also ensured that my Service Principal has the necessary permissions to access the model serving endpoint in the Databricks workspace.
However, when using this token to call the model serving API endpoint, I get the following response:
{
"error_code": 401,
"message": "Missing authorization details for accessing model serving endpoints"
}
Additional Context: I have already assigned permissions to the Service Principal in Databricks. Iโm using the all-apis scope to cover the model serving endpoint. Has anyone encountered this issue or have suggestions on what could be causing the 401 error?