获取 401:查询 Azure AD Graph 的组端点时未经授权的响应 API

Getting 401: Unauthorized response when querying groups endpoint of Azure AD Graph API

我有一个在 Azure 中注册的 .Net Core 2.0 应用程序。

我需要确定登录用户是否是特定 Azure AD 组的成员。我还需要仅使用群组名称来检索群组 ID。

为了检索组 ID,我正在向

发出 HttpWebRequest
https://graph.windows.net/myorganization/groups?api-version=1.6&$filter=displayName eq 'My group name' 

我正在传递一个有效的访问令牌。我说它是有效的,因为我使用此令牌成功地从 Power BI API 检索数据。

每次执行此请求时,我都会收到 401: Unauthorized 响应。

应用注册具有以下委托的 Azure AD Graph API 权限:Read all users' basic profilesRead all groupsRead directory data

我在这里错过了什么?

如果令牌适用于 Power BI REST API,那么它肯定不适用于 Azure AD Graph API。

Azure AD 中的访问令牌仅适用于单个 API,由令牌中的受众 (aud) 声明标识。

您需要在获取令牌时使用资源 https://graph.windows.net 为 Azure AD Graph API 获取另一个令牌。

您使用的 API 是 Azure AD Graph API,而不是 Microsoft Graph API。 MS Graph API 具有资源标识符 https://graph.microsoft.com,可通过 https://graph.microsoft.com.

访问

MS Graph API Documentation