【问题标题】:Cannot make HTTP requests after acquiring authorization token获取授权令牌后无法发出 HTTP 请求
【发布时间】:2017-07-13 19:45:01
【问题描述】:

我正在尝试使用 ADAL python 包访问 MSGraph API。这是我如何获取授权令牌的代码 sn-p。

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

现在,我正在使用这个令牌来发出一个 http 请求,如下所示:

request_url = 'https://graph.microsoft.com/v1.0/users'
h = {'Authorization': 'Bearer ' + token['accessToken']}
response = requests.get(url = request_url, headers = h)

我收到这样的回复:

{
  "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 应用注册门户中应用了必要的权限。我还设法使用Microsoft Graph Python connect sample 获得了这个请求的一个实例,所以我知道我能够访问数据。为什么我不能让这个实例工作?谢谢。

【问题讨论】:

    标签: python azure-active-directory microsoft-graph-api


    【解决方案1】:

    错误显示访问令牌没有足够的权限来执行列表用户 api 。您可以为您的应用授予Read all users' full profiles(User.Read.All 范围)应用权限

    1. 在您的 azure 广告应用程序中添加 microsoft graph 的 Read all users' full profiles 应用程序权限:

    2. 使用您的 AAD 管理员帐户单击上面屏幕截图中的 Grant Permissions 按钮授予该应用程序权限。

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

    4. 使用访问令牌发送列表用户 api。

    如果有帮助请告诉我。

    【讨论】:

    • 您好,感谢您的回复。我在 AAD 中授予了权限和所有内容,但现在我得到了另一个奇怪的响应。我收到一个“BadRequest”,上面写着“当前经过身份验证的上下文对此请求无效”。你知道为什么会这样吗?
    • @JohnM.Kovachi ,哪个请求会引发错误?您可以使用 fiddler 或 IE/Chrome 开发工具(网络部分)来捕获请求。
    猜你喜欢
    • 2015-02-13
    • 2017-12-26
    • 2018-01-05
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多