【问题标题】:Getting OAuth2 token using username&passowrd grant type使用用户名和密码授权类型获取 OAuth2 令牌
【发布时间】:2017-01-25 15:08:25
【问题描述】:

我正在使用以下 adal 库来获取 Oauth2 令牌 (AAD)。

https://github.com/AzureAD/azure-activedirectory-library-for-python

以下代码确实有效,我可以获得我的令牌: https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/certificate_credentials_sample.py

但在我的具体情况下,我希望能够通过提供用户名和密码(密码授予类型)来获取令牌

有什么想法吗?

谢谢,

【问题讨论】:

    标签: python azure adal


    【解决方案1】:

    没有很好的文档记录,但用户/密码身份验证在最新 (0.4.4) 中仍然可用

    context = adal.AuthenticationContext('https://login.microsoftonline.com/common')
    context.acquire_token_with_username_password(
        'https://management.core.windows.net',
        'me@outlook.com',
        'password',
        '04b07795-8ddb-461a-bbee-02f9e1bf7b46')
    

    参考:https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/adal/authentication_context.py#L128-L145

    【讨论】:

      【解决方案2】:

      由于某种原因,该库在最新版本中删除了使用用户名密码执行身份验证的可能性。

      但是使用 3.13.8.999 这个版本的 Microsoft.IdentityModel.Clients.ActiveDirectory nuget 包,它不是最新的,但正是你需要的。

      然后就这样下去

      string tenantname = ConfigurationManager.AppSettings["ida:Tenant"];
      string clientId = ConfigurationManager.AppSettings["ida:ClientID"];
      
      string authority = $"https://login.microsoftonline.com/{tenantname}";
      var authenticationContext = new AuthenticationContext(authority, null);
      
      string username = $"{UserName}@{tenantname}";
      var userCred = new UserPasswordCredential(username, Password);
      
       AuthenticationResult authResult = await   authenticationContext.AcquireTokenAsync(clientId, clientId, userCred);
      

      希望这就是你要找的。​​p>

      【讨论】:

      • 谢谢,我可以在 python 中做同样的事情来降低包版本吗?
      • 我想是的。但我不是 python 专家.. 虽然最近在 c# 中做过。
      猜你喜欢
      • 2015-03-09
      • 2020-03-04
      • 1970-01-01
      • 2019-08-31
      • 2018-06-23
      • 1970-01-01
      • 2013-11-08
      • 2017-01-04
      • 2011-09-16
      相关资源
      最近更新 更多