python - 微软合作伙伴生成令牌
问题描述
使用python我做到了这一点。但无法生成有助于获取客户数据的代币。获取访问令牌的代码
url = "https://login.microsoftonline.com/"+tanat_id+"/oauth2/token"
headers = {
"Accept": "application/json",
"return-client-request-id": "true",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Host": "login.microsoftonline.com",
"Content-Length": "194",
"Expect": "100-continue"
}
data = {
'client_id': client_id,
'resource': 'https://api.partner.microsoft.com',
'client_secret': client_secret,
'grant_type': 'client_credentials',
}
res = requests.post(url, headers=headers, data=data)
access_token = json.loads(res.text)["access_token"]
生成令牌的代码
url = "https://api.partnercenter.microsoft.com/generatetoken"
headers = {
"Authorization": "Bearer " + str(access_token),
"Accept": "application/json",
"content-type": "application/json;charset=UTF-8"
}
data = {
'grant_type': 'authorization_code',
}
res = requests.post(url, headers=headers, data={"grant_type":"jwt_token"})
但
>>> res.text
'{"error":"invalid_grant","error_description":"Invalid token: tokenValidationResult == null - True, tokenValidationResult.Principal == null - True, tokenValidationResult.Principal.Identity == null- True, tokenValidationResult.Principal.Identity.IsAuthenticated - "}'
如果我尝试获取客户列表,则使用 access_token 会返回 401 错误。
#Customer List
url = "https://api.partnercenter.microsoft.com/v1/customers?size=40"
headers = {
"Authorization": "Bearer " + str(access_token),
"Accept": "application/json",
}
res = requests.get(url, headers=headers)
res
<Response [401]>
res.text
""
解决方案
您需要从https://graph.windows.net获取第一个访问令牌
data = {
'client_id': client_id,
'resource': 'https://graph.windows.net',
'client_secret': client_secret,
'grant_type': 'client_credentials',
}
请在此处检查请求标头
推荐阅读
- firebase - 使用集合创建一个 Firestore 文档
- outlook - SendGrid 不向 Outlook 发送电子邮件
- html - 如何在引导折叠/手风琴中正确制作固定标题?
- rust - How to uninstall Rust that was installed via rustup?
- javascript - Get First Sentence with Regex (JavaScript)
- puppet - 使用 Puppet 设置文件模式
- ios - Spritekit 2.5d 如何为越来越近的物体设置动画(例如 Temple Run)
- html - 在引导程序中定位输入按钮和文本
- angular - Angular 6调用服务未在表单更改中返回数据
- jquery - Using Django, the selected option of a dropdown menu is not passed to the server