python - Microsoft 合作伙伴生成 token

标签 python azure api token

我使用 python 做到了这一点。但无法生成有助于获取客户数据的代币。 获取访问 token 的代码

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"]

生成 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 获取第一个访问 token

data = {
    'client_id': client_id,
    'resource': 'https://graph.windows.net',
    'client_secret': client_secret,
    'grant_type': 'client_credentials',
}

请检查请求 header here

关于python - Microsoft 合作伙伴生成 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68897504/

相关文章:

azure - 如何在 Azure 逻辑应用上使用 Gmail 附件

android - 上传 Google Drive API Retrofit 2

java - 为使用 android.os.Build 中的某些字段的 Android 应用程序编写测试

python - Flask URL 中的日期

python - 如何在 Python 上制作 3D 图形动画

c# - Azure 应用服务简单端点的响应非常慢

android - Azure 媒体服务的 MPEG-DASH 中出现错误

php - 网站管理员工具 API 和 PHP

python - sqlalchemy session.close() 不关闭连接

python - 如何从 pandas 样式对象中的日期时间列中删除时间戳