gpt4 book ai didi

azure-active-directory - 微软 Azure-OAuth2- "invalid_request"

转载 作者:行者123 更新时间:2023-12-05 08:40:44 24 4
gpt4 key购买 nike

我想将我的应用与 Microsoft Graph 连接。我在 Azure 中创建了我的网络应用程序(我有我的 client_idclient_secret)。我可以发送请求以从 https://login.microsoftonline.com/common/oauth2/v2.0/authorize 获取授权代码。

问题是,当我发送 POST 请求以从 https://login.microsoftonline.com/common/oauth2/v2.0/token 获取访问 token 时(与“使用权限”部分中所说的 here 完全一样)使用 Postman(使用 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"
}

此外,当我在 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"
}

但是,当我在 Postman 的 header 中删除 application/json 并添加 x-www-form-urlencoded 选项时,一切看起来都很好。

我只能在我的应用程序中发送具有 JSON 格式的 POST 请求。

Microsoft Graph 是否支持 JSON 格式的 POST 请求?

是 Postman 的问题吗?

最佳答案

我遇到了类似的问题,但意识到 Content-Type: application/x-www-form-urlencoded header 与 JSON 格式的请求正文之间存在不匹配。如果你引用this documentation ,您会看到请求正文需要进行 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
}
}

希望对您有所帮助!

关于azure-active-directory - 微软 Azure-OAuth2- "invalid_request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com