我正在按照获得没有用户的访问权限指南编写一个叫做MicrosoftGraph的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"
}
}
}以下是我选择的权限:

知道怎么纠正这个错误吗?
发布于 2018-03-28 19:04:12
首先,您可以删除所有委托的权限范围。如果使用的是客户端凭据授予,则只使用应用程序权限范围。
其次,在使用客户端凭据之前,需要执行Admin同意流。这是通过让租户进行全局管理员身份验证和接受您的范围请求来完成的:
https://login.microsoftonline.com/common/adminconsent?client_id=[APPLICATION ID]&redirect_uri=[REDIRECT URI]您可以在这里阅读更多关于行政许可的内容:v2端点与行政同意
发布于 2019-03-12 23:41:12
对于其他遇到这个问题的人,在发现文档遗漏了一个非常重要的警告之前,我也会得到这个错误:
https://login.microsoftonline.com/common/oauth2/token,将common替换为tenantId或域名https://stackoverflow.com/questions/49522572
复制相似问题