You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Web应用托管的FastAPI启用托管标识后,使用WorkspaceClient默认凭据连接Databricks失败

Azure Web应用托管的FastAPI启用托管标识后,使用WorkspaceClient默认凭据连接Databricks失败

我遇到了个棘手的问题,想请大家帮忙看看:我有一个部署在Azure Web App上的FastAPI,想要从Databricks拉取数据后通过接口返回。下面是我写的相关代码:

DbxContext.py 代码实现

import time
import pandas as pd
from databricks import sql
from databricks.sdk import WorkspaceClient
from pydantic import SecretStr
# from databricks.sdk.service import users

class DataBricksOptions:
    def __init__(self, client_id: str, client_secret: SecretStr, tenant_id: str, workspace_resource_id: str, http_path: str, host: str): 
        self.client_id = client_id 
        self.client_secret = client_secret 
        self.tenant_id = tenant_id 
        self.workspace_resource_id = workspace_resource_id 
        self.http_path= http_path
        self.host = host

class DataBricksContext:
    def __init__(self, options: DataBricksOptions):
        self.options = options

    def get_databricks_connection(self):
        try:
            # Initialize Databricks Workspace Client using default credentials
            w = WorkspaceClient()
            me2 = w.current_user.me()
            print(f"Me: {me2.display_name}")
            #w.users.me()
            # Create Databricks Token
            token = w.tokens.create(comment=f"sdk-{time.time_ns()}", lifetime_seconds=3600)

            # Establish SQL Connection
            connection = sql.connect(
                server_hostname=self.options.host,
                http_path=self.options.http_path,
                access_token=token.token_value
            )
            return connection
        except Exception as e:
            print(f"Error in get_databricks_connection(): {e}")
            return None

    def exec_query_to_df(self, connection, query: str):
        try:
            # Use the connection to execute the query
            df = pd.read_sql(query, connection)
            return df
        except Exception as e:
            print(f"Error in exec_query_to_df(): {e}")
            return None

这段代码在本地VS Code里跑完全正常,但部署到Azure Web App之后就报错了,错误信息如下:

Error in get_databricks_connection(): default auth: cannot configure
default credentials, please check
to configure credentials for your preferred authentication

对了,我已经在这个Azure Web App上开启了系统分配标识,但还是遇到这个问题,有没有大佬能给点解决建议呀?


备注:内容来源于stack exchange,提问作者Thomas Segato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:09:33