python - 适用于 Python 的 Azure ADAL - 面临 acquire_token_with_username_password 方法的问题

标签 python azure adal azure-sdk-python

我正在尝试使用 azure 用户 ID 和密码检索访问 token 。 最初我尝试使用以下 python 代码块

import adal
context = adal.AuthenticationContext(AUTHORITY)
token = context.acquire_token_with_client_credentials(
    "https://management.azure.com/",
    CLIENT_ID,
    CLIENT_SECRET)

这将返回 token ,没有任何问题。 我正在遵循 https://github.com/AzureAD/azure-activedirectory-library-for-python 中的示例用于使用用户名和密码检索 token ,代码块如下

token2 = context.acquire_token_with_username_password("https://management.azure.com/",USER_NAME,PASSWORD,CLIENT_ID)

在这种情况下,不会返回 token ,而是返回以下响应。

File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 145, in acquire_token_with_username_password
    return self._acquire_token(token_func)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 109, in _acquire_token
    return token_func(self)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 143, in token_func
    return token_request.get_token_with_username_password(username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 286, in get_token_with_username_password
    token = self._get_token_username_password_federated(username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 252, in _get_token_username_password_federated
    username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 211, in _perform_username_password_for_access_token_exchange
    username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 198, in _perform_wstrust_exchange
    result = wstrust.acquire_token(username, password)
  File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\wstrust_request.py", line 160, in acquire_token
    raise AdalError(return_error_string, error_response)
adal.adal_error.AdalError: WS-Trust RST request returned http error: 500 and server response: <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2017-06-07T12:12:56.567Z</u:Created><u:Expires>2017-06-07T12:17:56.567Z</u:Expires></u:Timestamp></o:Security></s:Header><s:Body><s:Fault><s:Code><s:Value>s:Sender</s:Value><s:Subcode><s:Value xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:FailedAuthentication</s:Value></s:Subcode></s:Code><s:Reason><s:Text xml:lang="en-US">ID3242: The security token could not be authenticated or authorized.</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>

Process finished with exit code 1

如果有人对此有任何想法,请告诉我。

最佳答案

根据错误堆栈的信息,根据我的经验,由于错误来自方法 _get_token_username_password_federated 以及 WS-Trust RST 请求的 500 错误代码,看来您的用户/密码用于获取未在您注册的应用程序的 Azure AD 中创建的 token ,但看起来像是在联合 AD 中创建的 token 。

请尝试通过以下两种方式排查问题。

  1. 使用您的管理员帐户在 Azure 门户上注册的应用的 Azure AD 中创建一个新用户,然后使用新用户/密码检索 token 。
  2. 检查您的 Azure AD 配置,确保是否部署了 Active Directory 联合身份验证服务以及联合 AD 实例中创建的当前用户名/密码。

关于python - 适用于 Python 的 Azure ADAL - 面临 acquire_token_with_username_password 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44414375/

相关文章:

python 删除xml中的非标签

python - 在 Seaborn PairGrid 上 reshape 子图

azure - 如何在Azure Durable Function编排实例之间缓存数据?

c# - Azure 应用服务在应用程序之前终止 https?

python - 如何使用rest API查询Azure表存储

ios - Office 365 iOS SDK - 如何调用 SharePoint REST API

c# - AcquireTokenSilent 始终无法静默获取 token

python - 如何配置多个 Ubuntu Python 安装以避免 App Engine 的 SSL 错误?

azure - 是否可以使用 key 保管库 key 创建 JWT token ?

python - 使用未压缩的 RLE 为 COCO 数据集编码 numpy 数组