【Azure Developer】Python代码获取的Token出现'Authentication_MissingOrMalformed'问题
2021-12-04 16:40
531 查看
问题描述
Python 调用Azure AD中所注册的应用生成Token代码:
import requests, json client_id = 'yourclientid' client_secret = 'yourclientsecret' tokenUrl = 'https://login.chinacloudapi.cn/yourtenantid/oauth2/token?api-version=1.0' data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'resource': 'https://management.chinacloudapi.cn/', 'client_secret': client_secret } rToken = requests.post(tokenUrl, data=data) #print(rToken) # 404 not found token = rToken.json().get('access_token') headers = { 'Content-Type' : 'application\json', 'Authorization': 'Bearer {}'.format(token) } url = "https://management.chinacloudapi.cn/subscriptions/yoursubid/resourceGroups/yourgroupname/providers/Microsoft.Compute/virtualMachines/yourvmname?api-version=2020-06-01" r = requests.get(url, headers=headers) result = r.json() print(result)
在使用时候出现:
{'odata.error': {'code': 'Authentication_MissingOrMalformed', 'message': {'lang': 'en', 'value': 'Access Token missing or malformed.'}, 'requestId': '4a241f2e-997e-4eaa-23dd-807df708b81b', 'date': '2021-12-03T12:13:39'}}
问题分析
从错误消息来看,提示为Token错误。而AAD的Token都是JWT格式,所以可以使用任何在线解析工具直接查看JWT中的内容,第一是验证格式是否正确,第二是判断内容aud等内容是否匹配。
如 https://www.box3.cn/tools/jwt.html
所以:如果发现通过 JWT 解析 Token,发现受众aud是 https://management.chinacloudapi.cn,所以使用此Token是无法调用 https://graph.chinacloudapi.cn 的接口的,需要在获取Token的请求Data中把resource由
https://management.chinacloudapi.cn/ 修改为 https://graph.chinacloudapi.cn
data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'resource': 'https://graph.chinacloudapi.cn', 'client_secret': client_secret }
相关文章推荐
- 【Python小白】向PyPI上传代码出现问题,报401或者410如何解决(主要是上传的URL不正确)!
- 十次方项目登陆问题 token令牌解析,claims获取不到userid,求大神帮忙看下代码解决问题
- 解决Eclipse编写Python代码出现SyntaxError: Non-ASCII character问题
- ## Visual Studio 2019 写 python代码时出现:模块以及导入出现ImportError: No module named 'xxx'问题
- 解决使用python3.5来获取PCL库出现 unable to find vcvarsall.bat的问题
- notepad++编译器保存的python代码出现python:can't open file '**.py' :[error 2] No Such file or directory问题怎么解决
- python获取时间及时间格式转换问题实例代码详解
- TensorFlow学习笔记7——Win7下Spyder运行python代码出现UnicodeEncodeError问题的解决
- Springsecurity+Oauth2.0+JWT实现用户认证请求/token获取token 出现401无权限 问题
- 使用IdHttp获取网页代码出现的问题,只能得到部分代码,为什么,请大家帮忙
- python读取文件处理网页粘贴代码出现的问题
- Android开发之获取手机通讯录,按A-Z顺序排列,并解决Fortify代码扫描可能出现的CodeDex问题
- mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
- 在myeclipse7.0上部署openkm代码出现问题
- 在框架中获取视图的指针出现的问题
- 用NSIS安装日志install。log解决了一个安装出现隐蔽的问题、不用看代码,可以节省大把时间!开心!
- ubuntu12.04 64bit编译android ics4.0代码出现的/usr/bin/ld.bfd.real: cannot find 问题解决
- 如何解决mid使用过程中出现的以下问题(无效的过程调用或参数: 'mid')
- HTML代码中同时加入“onclick事件“和return false出现的小问题
- “超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。”问题的解决方法