发布于2021-08-09 13:20 阅读(150) 评论(0) 点赞(1) 收藏(4)
使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token。
如要了解如何从AAD中获取 client id,client secret,tenant id,请参考博文:【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret) 中的操作步骤一栏。
1)调用 ClientSecretCredential 方法,通过client_id, client_secret ,tenant_id 以及 authority=AzureAuthorityHosts.AZURE_CHINA,初始化 credentials 对象
2)调用对象中的 get_token方法,特别注意参数 scopes 的传递,如 "https://microsoftgraph.chinacloudapi.cn/.default", 如果缺少.default,则会提示参数错误(详见[遇见问题]部分)
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)
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 内容。已可读方式查看。
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 Class:https://docs.microsoft.com/zh-cn/python/api/azure-identity/azure.identity.azureauthorityhosts?view=azure-python
作者:龙膜授权
链接:http://www.phpheidong.com/blog/article/127158/dc520f48cc6dba74b766/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!