共用方式為


針對 Microsoft Entra 識別碼 (先前稱為 Azure Active Directory) 存取令牌進行疑難解答

本文說明如何針對取得 Microsoft Entra ID(先前稱為 Azure Active Directory)存取令牌,以及如何驗證存取令牌時可能會遇到的錯誤進行疑難解答。

無法使用使用者名稱和密碼取得令牌

錯誤訊息

The user or administrator has not consented to use the application with ID <client-id>.
Send an interactive authorization request for this user and resource.

解決方案

  1. 如果未將 AzureDatabricks 資源新增至您的應用程式,請要求 管理員 使用者加以新增。
  2. 使用互動式方法來取得令牌。 網頁將引導您授與應用程式的許可權。 或者,按兩下應用程式組態中所述的 [ 授與許可權 ] 按鈕。 授與許可權之後,您可以使用程序設計方法來取得令牌。

重新導向 URI 不相符

錯誤訊息

The reply url specified in the request does not match the reply urls configured for the application: '<application-id>'

解決方案

檢查要求中的重新導向 URI 是否符合應用程式中的其中一個。

驗證存取令牌

當您有 Microsoft Entra ID 存取令牌時,您可以確認它包含正確的資訊(請參閱 驗證令牌)。

您應該確認下欄位符合紀錄:

  • aud:Azure Databricks 資源標識符: 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d
  • iss:應該是 https://sts.windows.net/<tenant-id>/
  • tid:應該是工作區的租使用者(依組織標識符或工作區設備標識碼查閱此標識符)
  • nbf/exp:目前的時間應該落在 和 之間nbfexp
  • unique_name:除非使用者是工作區設備資源上的參與者,否則應該是 Databricks 工作區中存在的使用者

使用來自 OIDC 端點的公用憑證來驗證令牌的簽章。

以下是顯示令牌承載的代碼段。 您必須先使用 安裝 PyJWT 連結庫,並使用 來安裝密碼編譯連結庫pip install cryptographypip install pyjwt

import jwt

def decode_token(token):
  algorithm = jwt.get_unverified_header(token)['alg']

  decoded = jwt.decode(
    token,
    algorithms = [algorithm],
    options = {"verify_signature": False}
  )

  for key in decoded.keys():
     print(f"{key}: {str(decoded[key])}")

如果您想要完整譯碼令牌(包括簽章驗證),您可以使用下列代碼段。 您必須先使用 安裝 PyJWT 連結庫,並使用 來安裝密碼編譯連結庫pip install cryptographypip install pyjwt 此外,請務必在下列程式代碼中取代 <databricks-resource-id>

import jwt
import requests
from cryptography.x509 import load_pem_x509_certificate
from cryptography.hazmat.backends import default_backend

PEMSTART = '-----BEGIN CERTIFICATE-----\n'
PEMEND = '\n-----END CERTIFICATE-----\n'

# Get the Microsoft Azure public key.
def get_public_key_for_token(kid):
  response = requests.get(
    'https://login.microsoftonline.com/common/.well-known/openid-configuration',
  ).json()

  jwt_uri = response['jwks_uri']
  response_keys = requests.get(jwt_uri).json()
  pubkeys = response_keys['keys']

  public_key = ''

  for key in pubkeys:
    # Find the key that matches the kid in the token's header.
      if key['kid'] == kid:
        # Construct the public key object.
        mspubkey = str(key['x5c'][0])
        cert_str = PEMSTART + mspubkey + PEMEND
        cert_obj = load_pem_x509_certificate(bytes(cert_str, 'ascii'), default_backend())
        public_key = cert_obj.public_key()

  return public_key

# Decode the given <ms-entra-id> token.
def aad_access_token_decoder(access_token):
  header = jwt.get_unverified_header(access_token)
  public_key = get_public_key_for_token(header['kid'])
  # The value of the databricks_resource_id is as defined previously,
  # for example, databricks_resource_id = "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d"
  decoded = jwt.decode(
    access_token,
    key = public_key,
    algorithms = 'RS256',
    audience = '<databricks-resource-id>')

  for key in decoded.keys():
    print(f"{key}: {str(decoded[key])}")

以下是上述代碼段輸出的範例:

Azure registered app settings

如果令牌不敏感,您也可以透過在線 JWT 譯碼器來檢視譯碼的令牌。 在線譯碼器範例是 jwt.msjwt.io