Microsoft Graph API:Authorization_IdentityNotFound

Rya*_*yan 2 python microsoft-graph-api

我正在遵循没有用户指南的获取访问权限来编写将调用 Microsoft Graph 的 Python 脚本。

此脚本将从 cron 调度,因此它无法获得管理员同意(因此使用客户端凭据进行授权)。我能够使用此调用成功获取令牌:

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)
Run Code Online (Sandbox Code Playgroud)

然后,我尝试使用有效令牌通过此调用获取用户列表:

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)
Run Code Online (Sandbox Code Playgroud)

问题是我收到一个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"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

这些是我选择的权限:

graphPermsScreenshot

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

msc*_*rtz 5

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

  • 对于客户端凭据,如果应用程序属于工作或学校(组织)上下文,则https://login.microsoftonline.com/common/oauth2/token替换common为租户 ID 或域名

请参阅 Microsoft Graph API 请求中的 Authorization_IdentityNotFound