azure-active-directory - AADSTS5002710:无效的 JWT 令牌:标头格式错误
问题描述
我正在尝试在我的客户端 (ReactJS)、Express + Node.js 服务器 (API) 和 Microsoft Graph 之间实现“代表”流程。
到目前为止,我已经向微软(客户端)请求了一个 accessToken,并向我的 API 发出了请求。
我遇到了错误“AADSTS5002710:无效的 JWT 令牌:标头格式错误。” 当我尝试从我的 API 发出 Axios 发布请求时https://login.microsoftonline.com/tenantID/oauth2/v2.0/token
完全错误:
{
error: 'invalid_request',
error_description: 'AADSTS5002710: Invalid JWT token: header is malformed.\r\n' +
'Trace ID: 068a382b-6f83-40f6-b1b1-7134223f4500\r\n' +
'Correlation ID: f46a2c03-84e8-46b3-b9d6-467174befa0b\r\n' +
'Timestamp: 2021-01-06 16:26:40Z',
error_codes: [ 5002710 ],
timestamp: '2021-01-06 16:26:40Z',
trace_id: '068a382b-6f83-40f6-b1b1-7134223f4500',
correlation_id: 'f46a2c03-84e8-46b3-b9d6-467174befa0b'
}
我的请求正文根据教程“https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow”。
由于微软在线服务器,我不断收到上述错误。
我已经使用自己的自定义范围提出了原始(客户端)请求api://54ee17f...cfe06/Access.Test
解决方案
我按照教程在 Postman 中使用 On-Behalf-Of 流。但它运作良好。
我在这里的步骤:
- 将 Web API B 的 API 权限添加到 Web API A
- 使用身份验证代码流请求 Web API A 以获取访问令牌(
assertion
下一步)
得到
https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize
?scope={like api://1108f6-xxxxxxx-9f622/test} openid
&redirect_uri={redirect_uri of Web API A}
&nonce=123
&client_id={client-id of Web API A}
&response_type=id_token token
- 请求 Web API B 以获取 Microsoft Graph API 的访问令牌
邮政
https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
&client_id={client_id of Web API B}
&client_secret={client_secret}
&assertion={access token from previous step}
&scope=https://graph.microsoft.com/user.read offline_access
&requested_token_use=on_behalf_of
- 调用 Microsoft Graph API,例如
GET https://graph.microsoft.com/v1.0/users
.
您可以在https://jwt.io/中解码您的访问令牌(断言),然后检查HEADER
.
推荐阅读
- sql - 手动查询 vs 触发函数输出
- android - 电容器子模块作为空对象导入
- gradle - 如何排除所有 gradlew 文件的依赖关系并避免 /include 仅在 gradle 中的一个位置
- javascript - 导航到另一个页面到 vue 中的特定部分
- android - 安卓:数据绑定
- azure-data-factory - 在加载到 Cosmos DB 之前验证数据工厂中的 JSON 架构
- mongodb - 如何知道 mongodb 文档是否符合桶模式?
- c - 如果数组被释放,等于分配在堆上的数组元素的变量值会改变吗?
- regex - 删除字符串中的所有连续模式
- c# - 如何将字节数组作为类属性传递给 ASP.NET Web API 中的 OData 函数?