【问题标题】:Microsoft Graph API: Authorization_IdentityNotFoundMicrosoft Graph API:Authorization_IdentityNotFound
【发布时间】:2018-03-27 21:18:58
【问题描述】:

我正在按照Get access without a user 指南编写一个调用 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)

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

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

这些是我选择的权限:

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

【问题讨论】:

    标签: python microsoft-graph-api


    【解决方案1】:

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

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

    Authorization_IdentityNotFound on Microsoft Graph API request

    【讨论】:

    • 你是对的,你的答案补充了接受的答案。我必须用我的tenantID 替换 common(可以在 Azure 上找到)
    【解决方案2】:

    首先,您可以继续删除所有这些委托权限范围。如果您使用的是客户端凭据授予,您将只使用应用程序权限范围。

    其次,您需要先执行管理员同意流程,然后才能使用客户端凭据。这是通过让租户的全局管理员进行身份验证并接受您的范围请求来完成的:

    https://login.microsoftonline.com/common/adminconsent?client_id=[APPLICATION ID]&redirect_uri=[REDIRECT URI]
    

    您可以在此处阅读有关管理员同意的更多信息:v2 Endpoint and Admin Consent

    【讨论】:

    • 谢谢。这是我缺少的部分。我将您的同意 URL 粘贴到浏览器中并替换为虚假的 redirect_uri。同意过程因此出错,但看起来我已获得同意。 API 调用现在可以正常工作了!
    • 这对我来说很重要,但我还需要另一件事:用你可以在 Azure 上获得的真实租户替换tenant=common(概览选项卡)。
    猜你喜欢
    • 2020-04-27
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-26
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多