javascript - 对 Graph API 的 POST 请求因错误请求而失败

标签 javascript axios microsoft-graph-api

我正在开发一个脚本来访问图形 API 以读取我们 AD 中的所有用户。我正在通过 AXIOS 执行请求:

    let response;
    try {
        response = await axios.post(`https://login.microsoftonline.com/${config.get('azure.tenant')}/oauth2/v2.0/token`, {
            client_id: config.get('azure.appId'),
            scope: config.get('azure.scope'),
            client_secret: config.get('azure.secret'),
            grant_type: config.get('azure.grantType')
        });
    } catch (e) {
        res.send(e);
    }

但是,这失败了:

{
"message": "Request failed with status code 400",
"name": "Error",
"stack": "Error: Request failed with status code 400\n    at createError (C:\\_Apps\\repos\\timesAPI\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n    at settle (C:\\_Apps\\repos\\timesAPI\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (C:\\_Apps\\repos\\timesAPI\\node_modules\\axios\\lib\\adapters\\http.js:237:11)\n    at IncomingMessage.emit (events.js:194:15)\n    at endReadableNT (_stream_readable.js:1103:12)\n    at process._tickCallback (internal/process/next_tick.js:63:19)",
"config": {
"url": "https://login.microsoftonline.com/86xxxxxxxxx8df7b/oauth2/v2.0/token",
"method": "post",
"data": "{\"client_id\":\"18xxxxxxxxf5x\",\"scope\":\"https://graph.microsoft.com/.default\",\"client_secret\":\"/xxxxxxxS:6xxxxxsxxQ\",\"grant_type\":\"client_credentials\"}",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8",
"User-Agent": "axios/0.19.0",
"Content-Length": 184
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"m

axContentLength": -1
}
}

为此,我遵循以下教程:https://learn.microsoft.com/en-us/graph/auth-v2-service ,其中表示,通过以下方式访问 api:

// Line breaks are for legibility only.

POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token HTTP/1.1 Host: login.microsoftonline.com Content-Type: application/x-www-form-urlencoded

client_id=535fb089-9ff3-47b6-9bfb-4f1264799865 &scope=https%3A%2F%2Fgraph.microsoft.com%2F.default &client_secret=qWgdYAmab0YSkuL1qKv5bPX &grant_type=client_credentials

我只是发送了错误的 header 吗?

最佳答案

解决了。我使用字符串作为第二个参数发送了凭证:client_id=${config.get('azure.appId')}&scope=${config.get('azure.scope')}&client_secret=${config .get('azure.secret')}&grant_type=${config.get('azure.grantType')}

关于javascript - 对 Graph API 的 POST 请求因错误请求而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57266377/

相关文章:

React 中 Axios 调用时 API 中的 PHP-Session 发生变化

vba - 如何使用 VBA 调用 Microsoft Graph API?

c# - 如何使用 .Net Client SDK 从 Microsoft Graph 获取我所属的管理员角色?

javascript - 在 js/JQuery 中动态选择 ID

javascript - 如何在action类中识别两个不同的jsp页面按钮?如何编写条件,基于jsp页面查看按钮和查看全部按钮?

javascript - 遍历 json 对象

cors - Axios - 不存在 'Access-Control-Allow-Origin' header

javascript - 如何在 Axios 响应后创建一个数组并在 React js 中显示它

azure-active-directory - 无法在 microsoft graph API 上获取用户公司信息

javascript - 将对象推送到数组并使用另一个对象作为属性