azure - 在 Graph api 上使用客户端凭证流会返回没有范围的 access_token

标签 azure graph oauth-2.0 azure-active-directory microsoft-graph-api

这就是我的目标:我想从我的 api 中获取 access_token。目前我正在尝试使用客户端凭据流程获取访问 token : enter image description here

我成功得到回复: enter image description here

但是此响应没有执行任何操作,因为当我在 GraphApi 端点中使用 access_token 作为授权参数时,我收到错误响应,指出我缺少 scp 或 Roles 参数。我已从应用程序的管理员定义并批准了角色。可能是什么问题?

这是解码后的 jwt access_token:

{
  "aud": "a********************ed5",
  "iss": "https://sts.windows.net/**********4f-d6b581dd3836/",
  "iat": 1660825101,
  "nbf": 1660825101,
  "exp": 1660829001,
  "aio": "E2ZgYPits***********g8R9TAA==",
  "appid": "eabbd3*************da6990ed5",
  "appidacr": "1",
  "idp": "https://sts.windows.net/5a8f9d89-81da-4218-884f-d6b581dd3836/",
  "oid": "77e***************8085325",
  "rh": "0.AYEAiZ2PWtqBGEKIT9a1gd04NuDTu-rpzyZCiMRhXaaZDtWBAAA.",
  "sub": "77e4***************48085325",
  "tid": "5a8f9d89-8*****************581dd3836",
  "uti": "nl-kk3T*******w2ePAA",
  "ver": "1.0"
}

最佳答案

我尝试在我的环境中重现相同的结果并得到以下结果:

我为我的 Azure AD 应用程序创建了一个名为 DemoRole 的应用程序角色,如下所示:

转至 Azure 门户 -> Azure Active Directory -> 应用注册 -> 您的应用 -> 应用角色

enter image description here

我将该 DemoRole 分配给了几个用户,如下所示:

转至 Azure -> Azure Active Directory -> 企业应用程序 -> 您的应用 -> 用户和组

enter image description here

现在,我生成了带有如下参数的访问 token :

POST https://login.microsoftonline.com/common/oauth2/v2.0/token

client_id = xxxx-xxxx-xxxx
grant_type = client_credentials
client_secret = ***************
scope = api://xxxxxxx.xxxx.com/.default

回应:

enter image description here

当我解码 token 时,我也无法获得如下所示的角色:

enter image description here

要在解码 token 中获取角色声明,请确保添加您的自定义 API 权限并授予管理员同意,如下所示:

转至您的应用程序 -> API 权限 -> 添加权限 -> 我的 API -> 您的应用程序名称 -> 应用程序权限

enter image description here

授予管理员同意后,我再次生成了访问 token ,并在解码的 token 中获得了 roles 声明,如下所示:

enter image description here

关于azure - 在 Graph api 上使用客户端凭证流会返回没有范围的 access_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73403094/

相关文章:

c# - 生成 .rdlc 报告在本地有效,但在部署到 azure 后无效

sql-server - 如何在 SQL 连接字符串中使用 Azure KeyVault secret ?

algorithm - 带有扭曲的图形遍历算法 - 最小停止次数

azure - 如何定位deployment.yaml文件中的特定值?

c++ - Boost图分割错误

c - 支持边标签的图库

swift - PayPal SDK 请求访问 token 返回代码 401(未授权)

java - 访问 Youtube API

authentication - Spring-security/Grails 应用程序 - 未调用自定义 WebSecurity 配置

azure - 对 RowKey 值范围使用 Azure 表的词法过滤