在 Python 中获取客户端配置文件 Azure 的访问令牌
Get Access Token for client profile Azure in Python
我正在寻找一种在使用 Python 与 Azure 合作时从客户端配置文件获取访问令牌的方法。
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_cli_profile(ComputeManagementClient)
我从代码中获取了客户端配置文件上下文,但如何从中获取访问令牌?
我可以找到从客户端配置文件获取访问令牌的方法,要获取访问令牌,您可以使用 adal,使用哪种方法取决于您的要求。
例如,我使用客户端凭据获取服务主体的访问令牌以访问 Azure Management REST API
,给定的资源是 https://management.azure.com/
。
import adal
# Tenant ID for your Azure Subscription
TENANT_ID = 'xxxxxxx'
# Your Service Principal App ID
CLIENT = 'xxxxxxx'
# Your Service Principal Password
KEY = 'xxxxxxx'
subscription_id = 'xxxxxxx'
authority_url = 'https://login.microsoftonline.com/'+TENANT_ID
context = adal.AuthenticationContext(authority_url)
token = context.acquire_token_with_client_credentials(
resource='https://management.azure.com/',
client_id=CLIENT,
client_secret=KEY
)
print(token["accessToken"])
我正在寻找一种在使用 Python 与 Azure 合作时从客户端配置文件获取访问令牌的方法。
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_cli_profile(ComputeManagementClient)
我从代码中获取了客户端配置文件上下文,但如何从中获取访问令牌?
我可以找到从客户端配置文件获取访问令牌的方法,要获取访问令牌,您可以使用 adal,使用哪种方法取决于您的要求。
例如,我使用客户端凭据获取服务主体的访问令牌以访问 Azure Management REST API
,给定的资源是 https://management.azure.com/
。
import adal
# Tenant ID for your Azure Subscription
TENANT_ID = 'xxxxxxx'
# Your Service Principal App ID
CLIENT = 'xxxxxxx'
# Your Service Principal Password
KEY = 'xxxxxxx'
subscription_id = 'xxxxxxx'
authority_url = 'https://login.microsoftonline.com/'+TENANT_ID
context = adal.AuthenticationContext(authority_url)
token = context.acquire_token_with_client_credentials(
resource='https://management.azure.com/',
client_id=CLIENT,
client_secret=KEY
)
print(token["accessToken"])