Fab*_*ien 2 azure-active-directory postman microsoft-graph-api
我想将我的应用程序与 Microsoft Graph 连接。我在 Azure 中创建了我的网络应用程序(我有我的client_id和client_secret)。我可以发送请求以获取授权码https://login.microsoftonline.com/common/oauth2/v2.0/authorize。
问题是,当我使用 Postman(带选项)发送请求POST以获取访问令牌https://login.microsoftonline.com/common/oauth2/v2.0/token(正如 “使用权限”部分中所述form-data)时,我收到“AADSTS9000410:格式错误的 JSON”错误:
{
"error": "invalid_request",
"error_description": "AADSTS9000410: Malformed JSON.\r\nTrace ID: f5c1dd4b-ad43-4265-91cb-1b7392360301\r\nCorrelation ID: 1dea54ed-bb43-4951-bc9e-001877fe427b\r\nTimestamp: 2019-01-14 21:38:42Z",
"error_codes": [9000410],
"timestamp": "2019-01-14 21:38:42Z",
"trace_id": "f5c1dd4b-ad43-4265-91cb-1b7392360401",
"correlation_id": "1dea54ed-bb43-4951-bc9e-001878fe427b"
}
Run Code Online (Sandbox Code Playgroud)
此外,当我在 Postman 中使用原始选项发送相同的请求时,我收到“AADSTS900144:请求正文必须包含以下参数:'grant_type'”:
{
"error": "invalid_request",
"error_description": "AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID:a7c2f8f4-1510-42e6-b15e-b0df0865ff00\r\nCorrelation ID:e863cfa9-0bce-473c-bdf6-e48cfe2356e4\r\nTimestamp: 2019-01-1421:51:29Z",
"error_codes": [900144],
"timestamp": "2019-01-14 21:51:29Z",
"trace_id": "a7c2f8f4-1510-42e6-b15e-b0df0865ff10",
"correlation_id": "e863cfa9-0bce-473c-bdf6-e48cfe2356e3"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我application/json在 Postman 中删除标题并添加x-www-form-urlencoded选项时,一切看起来都很好。
我只能POST在我的应用程序中发送 JSON 格式的请求。
Microsoft Graph 是否支持 POST 请求的 JSON 格式?
是邮递员的问题吗?
Content-Type: application/x-www-form-urlencoded我遇到了类似的问题,但意识到标头和 JSON 格式的请求正文之间不匹配。如果您参考此文档,您会发现请求正文需要进行 URL 编码(与&符号、编码实体等连接),这最终解决了我的问题。因此,我不认为这是 Postman 或 MS API 的问题,而只是请求正文的格式不正确。
我不确定您的应用程序使用什么语言,但这里有一个适合我的使用 Node 和 Express 的示例:
const fetch = require('node-fetch')
const { URLSearchParams } = require('url')
async function getAccessToken(req, res, next) {
try {
const response = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
// Previously I was doing `body: JSON.stringify({...})`, but
// JSON !== URL encoded. Using `URLSearchParams` (or whatever
// the equivalent is in your language) is the key to success.
body: new URLSearchParams({
client_id: YOUR_CLIENT_ID_HERE,
scope: 'User.Read Calendars.Read',
redirect_uri: YOUR_REDIRECT_URL_HERE,
grant_type: 'authorization_code',
client_secret: YOUR_CLIENT_SECRET_HERE,
code: req.query.code
}
})
const json = await response.json()
// `json` will have `access_token` and other properties
} catch (err) {
throw err
}
}
Run Code Online (Sandbox Code Playgroud)
希望有帮助!
| 归档时间: |
|
| 查看次数: |
15092 次 |
| 最近记录: |