Python 获取授权令牌后无法进行HTTP请求

Python 获取授权令牌后无法进行HTTP请求,python,azure-active-directory,microsoft-graph-api,Python,Azure Active Directory,Microsoft Graph Api,我正在尝试使用ADAL python包访问MSGraph API。下面是关于如何获取授权令牌的代码片段 authority = "https://login.microsoftonline.com/" + tenant RESOURCE = "https://graph.microsoft.com" context = adal.AuthenticationContext(authority) #Use this for Client Credentials token = context.

我正在尝试使用ADAL python包访问MSGraph API。下面是关于如何获取授权令牌的代码片段

authority = "https://login.microsoftonline.com/" + tenant
RESOURCE = "https://graph.microsoft.com"

context = adal.AuthenticationContext(authority)

#Use this for Client Credentials
token = context.acquire_token_with_client_credentials(
    RESOURCE,
    client_id,
    client_secret
    )
在以以下形式提出此请求后,我将取回令牌:

{
  "tokenType": "Bearer",
  "_clientId": <clientID>,
  "_authority": "https://login.microsoftonline.com/<tenant>",
  "expiresIn": 3599,
  "isMRRT": true,
  "accessToken": <token>,
  "resource": "https://graph.microsoft.com",
  "expiresOn": "2017-07-13 16:38:11.591687"
}
我得到的回应如下:

{
  "error": {
    "code": "Authorization_RequestDenied",
    "message": "Insufficient privileges to complete the operation.",
    "innerError": {
      "request-id": "f8c634c0-e23a-4f46-8e39-70ed9e35ef68",
      "date": "2017-07-13T19:38:13"
    }
  }
}

我的问题是:为什么?我已在Microsoft应用程序注册门户中应用了必要的权限。我还设法通过使用获得了该请求的一个实例,因此我知道我能够访问数据。为什么我不能让这个实例工作?谢谢。

此错误显示访问令牌没有足够的权限执行list users api。您可以为您的应用程序授予
读取所有用户的完整配置文件
(User.Read.all scope)应用权限

  • 在azure ad应用程序中添加microsoft graph的
    读取所有用户的完整配置文件
    应用程序权限:
    

  • 通过使用AAD的管理员帐户单击上面屏幕截图中的
    Grant Permissions
    按钮来授予该应用程序权限

  • 使用客户端凭据流获取microsoft graph的访问令牌作为您的代码。获取访问令牌后,您可以使用解码令牌,您应该在
    角色
    声明中找到
    User.Read.All

  • 发送带有访问令牌的列表用户api


  • 如果有帮助,请告诉我。

    您好,谢谢您的回复。我在AAD中授予了权限和一切,但现在我得到了另一个奇怪的响应。我收到一个“BadRequest”,上面写着“当前经过身份验证的上下文对此请求无效”。你知道为什么会这样吗?@JohnM.Kovachi,哪个请求会抛出错误?你可以使用fiddler或IE/Chrome开发者工具(网络部分)来捕获请求。
    {
      "error": {
        "code": "Authorization_RequestDenied",
        "message": "Insufficient privileges to complete the operation.",
        "innerError": {
          "request-id": "f8c634c0-e23a-4f46-8e39-70ed9e35ef68",
          "date": "2017-07-13T19:38:13"
        }
      }
    }