我正在关注 Get access without a user编写将调用 Microsoft Graph 的 Python 脚本的指南。
此脚本将从 cron 安排,因此无法获得管理员同意(因此授权使用客户端凭据)。我能够使用此调用成功获取 token :
request_url = "https://login.microsoftonline.com/mytenant.onmicrosoft.com/oauth2/v2.0/token"
data = {
'Host' : 'login.microsoftonline.com',
'Content-Type' : 'application/x-www-form-urlencoded',
'client_id' : 'my-client-id-1234',
'scope' : 'https://graph.microsoft.com/.default',
'client_secret' : client_secret,
'grant_type' : 'client_credentials'
}
response = requests.post(url = request_url, data = data)
然后我尝试使用有效 token 通过此调用获取用户列表:
request_url = "https://graph.microsoft.com/v1.0/users"
headers = {
'Authorization' : 'Bearer ' + token,
'Host' : 'graph.microsoft.com'
}
response = requests.get(url = request_url, headers = headers)
问题是我收到 Authorization_IdentityNotFound
错误:
<Response [401]>
{
"error": {
"code": "Authorization_IdentityNotFound",
"message": "The identity of the calling application could not be established.",
"innerError": {
"request-id": "2257f532-abc4-4465-b19f-f33541787e76",
"date": "2018-03-27T19:11:07"
}
}
}
这些是我选择的权限:
知道如何解决这个错误吗?
最佳答案
对于遇到这个问题的其他人,我也遇到了这个错误,直到发现文档省略了一个非常重要的警告:
- 对于客户端凭据,如果应用属于工作或学校(组织)上下文,则对于
https://login.microsoftonline.com/common/oauth2/token
替换common
使用 tenantId 或域名
见 Authorization_IdentityNotFound on Microsoft Graph API request
关于python - 微软图形 API : Authorization_IdentityNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49522572/