我正在遵循没有用户指南的获取访问权限来编写将调用 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": …Run Code Online (Sandbox Code Playgroud)