我正在使用作业 API 来获取由 azure 服务主体创建的数据块作业的状态。我使用下面的代码创建了 azure 令牌
import requests
import json
from databricks_cli.sdk.service import JobsService
from databricks_cli.sdk.api_client import ApiClient
import time
#generating toekn request for service principal
sp_id = dbutils.secrets.get("scope name", "key")
sp_sec = dbutils.secrets.get("scope name", "key")
token_endpoint = "https://login.microsoftonline.com/<databricks-instance>/oauth2/token"
token_request_data = {
"grant_type":"client_credentials",
"client_id": sp_id,
"client_secret": sp_sec,
"scope" :"2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default"
}
#send the request to get the token
response = requests.post(token_endpoint, data= token_request_data)
token_sp= response.json()["access_token"]
然后我可以成功地使用这个令牌来运行数据块作业。但是,当我想使用相同的令牌获取作业的状态时,出现此错误:
#send the request using run ID to get the state of the job with service principal token
uri = "https://adb-1366553829746932.12.azuredatabricks.net/api/2.1/jobs/runs/get?run_id=831336"
headers = {
"Authorization":f"Bearer {token_sp}",
"Content-Type":"application/json",
}
response=requests.get(uri,headers = headers)
response.text
'<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>\n<title>Error 400 io.jsonwebtoken.IncorrectClaimException: Expected aud claim to be: 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d, but was: 00000002-0000-0000-c000-000000000000.</title>\n</head>\n<body><h2>HTTP ERROR 400</h2>\n<p>Problem accessing /api/2.1/jobs/runs/get. Reason:\n<pre> io.jsonwebtoken.IncorrectClaimException: Expected aud claim to be: 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d, but was: 00000002-0000-0000-c000-000000000000.</pre></p>\n</body>\n</html>\n'
如果我使用 PAT 而不是服务主体令牌,我可以获得工作状态。
我不确定为什么这个令牌适用于 POST 请求而不适用于 GET 请求。