Azure ML Python SDK无法通过身份密钥获取MSI令牌报错修复
- 基于Azure ML Python SDK开发管道工作流数周,近期调用获取默认数据存储接口时突发报错
- 运行环境:MacOS Monterey 系统下的conda虚拟环境
- Python版本:3.8
- Azure ML SDK版本:1.42.0
ws = Workspace.from_config() def_blob_store = ws.get_default_datastore()
Traceback (most recent call last): File "lstm_evaluate_pipeline.py", line 14, in
def_blob_store = ws.get_default_datastore() File "/opt/anaconda3/envs/azure_ml/lib/python3.8/site-packages/azureml/core/workspace.py", line 1154, in get_default_datastore
return _DatastoreClient.get_default(self) File "/opt/anaconda3/envs/azure_ml/lib/python3.8/site-packages/azureml/data/datastore_client.py", line 699, in get_default
return _DatastoreClient._get_default(workspace) File "/opt/anaconda3/envs/azure_ml/lib/python3.8/site-packages/azureml/data/_exception_handler.py", line 19, in decorated
raise UserErrorException(str(e)) azureml.exceptions._azureml_exception.UserErrorException:
UserErrorException: Message: (UserError) Unable to get MSI token using identity secret. The application associated with this managed identity InnerException None ErrorResponse {
"error": {
"code": "UserError",
"message": "(UserError) Unable to get MSI token using identity secret. The application associated with this managed identity"
} }
SDK默认认证链路逻辑异常,在本地非Azure托管环境下错误尝试使用MSI托管身份拉取令牌。MSI认证仅支持Azure云内托管资源(虚拟机、ML计算集群、函数应用等),本地Mac环境无对应托管身份绑定,最终触发认证失败。该问题通常出现在本地缓存的登录凭证过期、SDK依赖文件损坏场景下。
- 第一步:清空本地失效的Azure账号缓存,在终端执行命令:
az account clear - 第二步:重新完成Azure CLI登录,若使用多租户环境需追加租户ID参数:
az login
多租户场景使用:az login --tenant <你的Azure AD租户ID>
登录完成后确认当前选中订阅为Azure ML工作空间所在订阅。 - 第三步:修改代码,显式指定使用Azure CLI认证加载工作空间,跳过SDK自动探测MSI的逻辑,修改后代码如下:
from azureml.core import Workspace from azureml.core.authentication import AzureCliAuthentication # 显式传入CLI认证对象,避免误触发MSI认证流程 cli_auth = AzureCliAuthentication() ws = Workspace.from_config(auth=cli_auth) def_blob_store = ws.get_default_datastore()
- 第四步:若以上操作后仍报错,说明当前conda环境内SDK依赖存在损坏,执行强制重装对应版本SDK:
pip install --force-reinstall azureml-core==1.42.0
注意:本地开发环境无需配置MSI认证,该认证方式无法在非Azure托管的本地设备上生效。
内容的提问来源于stack exchange,提问作者gary69

