python - 微软图形 API : Authorization_IdentityNotFound

标签 python microsoft-graph-api

我正在关注 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"
      }
   }
}

这些是我选择的权限:

graphPermsScreenshot

知道如何解决这个错误吗?

最佳答案

对于遇到这个问题的其他人,我也遇到了这个错误,直到发现文档省略了一个非常重要的警告:

  • 对于客户端凭据,如果应用属于工作或学校(组织)上下文,则对于 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/

相关文章:

python - locals 字典是什么时候设置的?

python - 语法怪癖或为什么有效的 python

python - 服务 'web' 无法构建,协议(protocol)错误

python - 如何将显示层次关系的字典列表转换为树?

microsoft-graph-api - 如何在群聊中正确显示帖子内容?

azure - 如何使用graph api知道当前登录的用户是否是租户管理员

python - 执行 Plotly Dash .exec 文件时出现 PyInstaller 错误

php - 如何为已登录的用户获取 Azure AD 访问 token ?

azure - 无需用户交互即可检索 OAuth 2.0 授权代码

Azure 应用程序注册 Microsoft Graph 权限异常