【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式

简介: 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式

问题描述

使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token。

如要了解如何从AAD中获取 client id,client secret,tenant id,请参考博文:【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret) 中的操作步骤一栏。

代码展示

获取方式一:使用 azure.identity

1)调用 ClientSecretCredential 方法,通过client_id, client_secret ,tenant_id 以及 authority=AzureAuthorityHosts.AZURE_CHINA,初始化 credentials 对象

2)调用对象中的 get_token方法,特别注意参数 scopes 的传递,如 "https://microsoftgraph.chinacloudapi.cn/.default", 如果缺少.default,则会提示参数错误(详见[遇见问题]部分)

print("方式一: ClientSecretCredential")
from azure.identity import ClientSecretCredential
credentials = ClientSecretCredential(client_id='xxxxxxxx-xxxx-xxxx-xxxx-76f50363af33', client_secret='.~V9ij1.5Y_F8rL_k8DNpj~RSLFf~H56nH', tenant_id='xxxxxxxx-xxxx-xxxx-xxxx-1316152d9587',authority=AzureAuthorityHosts.AZURE_CHINA)
token =credentials.get_token("https://microsoftgraph.chinacloudapi.cn/.default")
print(token)

 

调用方式二:使用 azure.common.credentials

1) 调用 ServicePrincipalCredentials 方法,同样通过参数 client_id, secret, tenant, resource 和 china='true' , 初始化 credentials 对象

2) 解析credentials对象,获取Token中的 access_token属性值。credentials.token['access_token']

print("方式二: ServicePrincipalCredentials")
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(client_id='xxxxxxxx-xxxx-xxxx-xxxx-76f50363af33', secret='.~xxxx.xxxx~xxxx~xxxx', tenant='xxxxxxxx-xxxx-xxxx-xxxx-1316152d9587', resource='https://microsoftgraph.chinacloudapi.cn/', china='true')
access_token = credentials.token['access_token']
print(access_token)

 

方式一和方式二执行的结果相同

 

PS: 使用 https://jwt.io/ 可以Decoded token 内容。已可读方式查看。

 

 

遇见问题

错误一:get_token 提示 requires at least one scope。

Traceback (most recent call last):
  File "client.py", line 7, in <module>
    print(credentials.get_token(scopes=""))
  File "C:\Users\bulu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\azure\identity\_internal\get_token_mixin.py", line 64, in get_tokent_token_mixin.py", line 64, 
in get_token    raise ValueError('"get_token" requires at least one scope')
ValueError: "get_token" requires at least one scope

错误的原因就是输入的scope参数不正确。需要输入“https://microsoftgraph.chinacloudapi.cn/.default" 携带.default。

The /.default scope is built in for every application that refers to the static list of permissions configured on the application registration. Source: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#the-default-scope

 

 

参考资料

The /.default scope: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#the-default-scope

identity Package: https://docs.microsoft.com/zh-cn/python/api/azure-identity/azure.identity?view=azure-python

AzureAuthorityHosts Classhttps://docs.microsoft.com/zh-cn/python/api/azure-identity/azure.identity.azureauthorityhosts?view=azure-python

 

相关文章
|
7月前
|
SQL 自然语言处理 数据库
【Azure Developer】分享两段Python代码处理表格(CSV格式)数据 : 根据每列的内容生成SQL语句
本文介绍了使用Python Pandas处理数据收集任务中格式不统一的问题。针对两种情况:服务名对应多人拥有状态(1/0表示),以及服务名与人名重复列的情况,分别采用双层for循环和字典数据结构实现数据转换,最终生成Name对应的Services列表(逗号分隔)。此方法高效解决大量数据的人工处理难题,减少错误并提升效率。文中附带代码示例及执行结果截图,便于理解和实践。
170 4
|
2月前
|
JavaScript 前端开发 机器人
【Azure Bot Service】在中国区Azure上部署机器人的 Python 版配置
本文介绍了在中国区Azure上使用Python SDK配置Azure Bot Service时遇到的问题及解决方案,涵盖参数设置与适配器配置,适用于希望在Azure中国区部署Python机器人的开发者。
|
5月前
|
API 开发工具 网络架构
【Azure Service Bus】使用Python SDK创建Service Bus Namespace资源(中国区)
本文介绍了如何使用Python SDK创建Azure Service Bus Namespace资源。首先,通过Microsoft Entra ID注册应用获取Client ID、Client Secret和Tenant ID,完成中国区Azure认证。接着,初始化ServiceBusManagementClient对象,并调用`begin_create_or_update`方法创建资源。
120 29
|
6月前
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
173 23
|
7月前
|
API 开发工具 Python
|
8月前
|
API 开发工具 Python
【Azure Developer】编写Python SDK代码实现从China Azure中VM Disk中创建磁盘快照Snapshot
本文介绍如何使用Python SDK为中国区微软云(China Azure)中的虚拟机磁盘创建快照。通过Azure Python SDK的Snapshot Class,指定`location`和`creation_data`参数,使用`Copy`选项从现有磁盘创建快照。代码示例展示了如何配置Default Azure Credential,并设置特定于中国区Azure的`base_url`和`credential_scopes`。参考资料包括官方文档和相关API说明。
105 1
|
22天前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
191 102
|
22天前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
194 104
|
22天前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
185 103
|
22天前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
131 82

推荐镜像

更多