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/

相关文章:

javascript - 在 Echarts 中保存为图像时的图形标题

javascript - 莫里斯线图设置数据方法无法正常工作

java - 为什么不推荐使用 GetServerAuthCodeResult?如何在已安装的应用程序中执行等效操作?

asp.net-mvc - 一起使用不记名 token 和 cookie 身份验证

visual-c++ - 将 VC++ 程序集部署到 Azure

azure - Spring boot 微服务(Rest Api)作为 azure function-Azure function +spring Rest 微服务

Azure - 在 Java Spring Boot 中使用用户管理身份访问 Key Vault : Error Details: ManagedIdentityCredential authentication unavailable?

azure - Kusto 查询语言拆分 @ 字符并获取最后一项

Java像图一样遍历一个大的HashMap

java - 在 Android 上设置谷歌登录时出现 "FirebaseError: Invalid authentication credentials provided"错误