Azure AAD 和 Graph API:权限不足,无法完成操作

Azure AAD and Graph API: Insufficient privileges to complete the operation

上下文:我有一个控制台应用程序,它想使用 Graph API 与 AAD 通信以检查租户中是否存在特定的 userId。

我一直遵循这里的指南:https://docs.microsoft.com/en-us/graph/auth-v2-service?view=graph-rest-1.0

我可以使用这个生成令牌:

https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id=x
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret=x
&grant_type=client_credentials

但是当我调用图表时 API 我得到这个错误:

https://graph.microsoft.com/v1.0/users/12345678-73a6-4952-a53a-e9916737ff7f
{
    "error": {
        "code": "Authorization_RequestDenied",
        "message": "Insufficient privileges to complete the operation.",
        "innerError": {
            "request-id": "x",
            "date": "x"
        }
    }
}

我的 AAD 应用拥有以下所有权限:

1. Microsoft Graph
2. Windows Azure Active Directory

我尝试将范围更改为

scope=https%3A%2F%2Fgraph.microsoft.com%2Fuser.read

但这是我在生成令牌时遇到的错误:

The provided value for the input parameter 'scope' is not valid. The scope https://graph.microsoft.com/user.read is not valid.

我尝试了“User.Read”、“User.Basic.Read”等组合,但没有任何效果。

您创建的应用程序是否对该范围具有委派权限或应用程序权限?

很有可能是前者。委派的权限不适用于客户端凭据流。

这不起作用的最可能原因是您将应用注册配置为需要的权限实际上并未授予 由您组织的管理员提供。

在您的代码中,您的应用程序作为应用程序进行身份验证。不涉及登录用户,它要求您的应用使用并保密用于身份验证的密钥(client_secret 参数)。

在这种情况下,请求范围 https://graph.microsoft.com/.default 是正确的方法。你对 Azure AD 说的是:"please provide an access token for all the application permissions this app has been granted"。请求范围 https://graph.microsoft.com/User.Read 不是正确的方法,因为不存在具有该名称的 application 权限。