繁体   English   中英

如何在python中使用ADAL使用Azure区块链工作台的用户名和密码获取身份验证承载?

[英]How do you get authentication bearer using a username and password for the Azure Blockchain Workbench using ADAL in python?

我一直在尝试从Python在我的Azure区块链工作台上执行一些合同。 我一直无法弄清楚如何使用此方法。 adal.acquire_token_with_username_password()

我首先需要执行身份验证以获取进行进一步API调用的载体。 context.acquire_token_with_client_credentials(client_id,client_id,client_secret)不过,上述不记名令牌与任何注册用户都不相关。

但是,要执行诸如添加新用户之类的管理任务,必须获得管理帐户的载体。 因此,我想到了使用acquire_token_with_username_password()来获取管理帐户的载体。

import adal
import swagger_client
from swagger_client.api_client import ApiClient
context = adal.AuthenticationContext("https://login.microsoftonline.com/kumarshobhit98outlook.onmicrosoft.com/",api_version=None)
client_id="c62087b9-cfed-4105-a9c2-4fd3953ceed5"
token = context.acquire_token_with_username_password(resource='https://graph.windows.net',username="shobhit@kumarshobhit98outlook.onmicrosoft.com",password="password",client_id=client_id)
print(token['accessToken'])

我猜可能Resource参数不正确。 我不知道该参数的含义。 另外这是我得到的错误,

Traceback (most recent call last):
  File "f:/codefundo2019/voting-system-blockchain/contractsShobhit/python/regVoter.py", line 8, in <module>
    token = context.acquire_token_with_username_password(resource='https://graph.windows.net',username="shobhit@kumarshobhit98outlook.onmicrosoft.com",password="Alonso123",client_id=client_id)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\authentication_context.py", line 164, in acquire_token_with_username_password
    return self._acquire_token(token_func)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\authentication_context.py", line 128, in _acquire_token
    return token_func(self)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\authentication_context.py", line 162, in token_func
    return token_request.get_token_with_username_password(username, password)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\token_request.py", line 281, in get_token_with_username_password
    token = self._get_token_username_password_managed(username, password)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\token_request.py", line 177, in _get_token_username_password_managed
    return self._oauth_get_token(oauth_parameters)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\token_request.py", line 112, in _oauth_get_token
    return client.get_token(oauth_parameters)
  File "C:\Users\SHOBHIT KUMAR.SHOBHIT-PC.000\.conda\envs\test\lib\site-packages\adal\oauth2_client.py", line 289, in get_token
    raise AdalError(return_error_string, error_response)
adal.adal_error.AdalError: Get Token request returned http error: 401 and server response: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: 2492ffdd-46e6-4edb-a412-47eefd200a00\r\nCorrelation ID: 2bbb1de3-b0b8-4510-b723-237e2faa7163\r\nTimestamp: 2019-08-09 06:50:11Z","error_codes":[7000218],"timestamp":"2019-08-09 06:50:11Z","trace_id":"2492ffdd-46e6-4edb-a412-47eefd200a00","correlation_id":"2bbb1de3-b0b8-4510-b723-237e2faa7163"}

我不明白为什么要为用户名密码方法要求一个client_secret

但是,要执行诸如添加新用户之类的管理任务,必须获得管理帐户的载体。

这是不正确的。 令牌权限与帐户无关,但与您授予应用程序的权限有关。 例如,如果您要调用add new user api 您将需要User.ReadWrite.All权限。

在此处输入图片说明

转到Azure门户-> Azure Active Directory->应用程序注册->查找应用程序-> Api权限->添加权限-> Microsoft Graph->应用程序权限->选择User.ReadWrite.All权限->授予管理员同意。

我猜可能Resource参数不正确。 我不知道参数的含义

这是目标Web API(安全资源)的App ID URI。 它也可能是外部资源,例如https://graph.microsoft.com 您使用https://graph.windows.net设置。 然后,您将只能调用Azure AD图形api

我不明白为什么它要求使用client_secret作为用户名密码方法。

您需要将应用程序视为公共客户端。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM