<p>我有几种方法可以做到这一点,但没有一种方法特别漂亮。冒着天真建议的风险,如果您希望对顺序中的工作进行细粒度的理解,您可以采取类似于您的示例使用列出的组件凭据所绘制的方法<a href="https://docs.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet" rel="nofollow noreferrer">here</a>:</p>
<pre class="lang-py prettyprint-override"><code>for credential in [EnvironmentCredential, ManagedIdentityCredential, ...]:
try:
credentials = credential()
# Validate credential functionality...
logging.info('{}.get_token succeeded'.format(credential))
except Error1 as e1:
logging.error('{} failed due to {}'.format(credential, e1))
</code></pre>
<p>我建议这样做是因为它看起来不像是以一种易于访问的方式公开您想要的内容,查看<code>ChainedCredential</code>源代码<a href="https://github.com/Azure/azure-sdk-for-python/blob/7a730caaeb6ca118a95b86df08448b2aac9713ae/sdk/identity/azure-identity/azure/identity/_credentials/chained.py#L67" rel="nofollow noreferrer">here</a>(历史是建立的,但只在日志消息中使用)</p>
<p>更多的“自包含”方法可能涉及创建一个自定义包装器,它基本上完成了<code>ChainedCredential</code>在运行get_令牌调用列表时为<code>DefaultAzureCredential</code>所做的工作,但如果能够导出成功历史记录,您可能会从我在<a href="https://github.com/Azure/azure-sdk-for-python/blob/7a730caaeb6ca118a95b86df08448b2aac9713ae/sdk/identity/azure-identity/azure/identity/_credentials/chained.py#L67" rel="nofollow noreferrer">2</a>中链接的代码中删除它,并做一些与<a href="https://github.com/Azure/azure-sdk-for-python/blob/7a730caaeb6ca118a95b86df08448b2aac9713ae/sdk/identity/azure-identity/azure/identity/_credentials/default.py" rel="nofollow noreferrer">the DefaultAzureCredential logic</a>直接类似的事情,但归根结底,这都是上述样本的同构</p>
<p>如果我误解了问题的某些方面或您的要求/限制,请不要犹豫,大声喊叫;如果您需要与我们联系,请随时联系我们<a href="https://github.com/Azure/azure-sdk-for-python" rel="nofollow noreferrer">on our github</a>(完全公开,我是其他一些Python Azure SDK的维护者)</p>