gpt4 book ai didi

javascript - 如何将 Active Directory token curl 命令转换为节点获取

转载 作者:行者123 更新时间:2023-12-04 07:40:18 24 4
gpt4 key购买 nike

当我将自己的租户 ID、客户端 ID 和客户端 key 放入时,CURL 命令可以成功运行:

# Replace {tenant} with your tenant!
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=535fb089-9ff3-47b6-9bfb-4f1264799865&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=qWgdYAmab0YSkuL1qKv5bPX&grant_type=client_credentials' 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token'

我尝试使用节点获取时出错:

const fetch = require('node-fetch');
let tenantId='<my tenant id>';

let token = fetch(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
client_id: '<my client id>',
scope: 'https://graph.microsoft.com',
client_secret: '<my client secret>',
grant_type: 'client_credentials',
})
}).then(function(response) {
return response.json()
}).then(json => {
console.log(json)
})

我收到的错误:

  error: 'invalid_request',
error_description: "AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\n" +
'Trace ID: <trace id>\r\n' +
'Correlation ID: <correlation id>\r\n' +
'Timestamp: 2021-05-12 22:27:30Z',
error_codes: [ 900144 ],
timestamp: '2021-05-12 22:27:30Z',
trace_id: '<trace id>',
correlation_id: '<correlation id>',
error_uri: 'https://login.microsoftonline.com/error?code=900144'

我的 node-fetch POST 请求中的正文有什么问题?

顺便说一句,我尝试过 Axios,也得到了相同的结果。

最佳答案

AD 正在等待表单请求,请尝试此操作

const fetch = require('node-fetch');
let tenantId='<my tenant id>';

let token = fetch(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`, {
method: 'post',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'client_id=<my client id>&scope=https://graph.microsoft.com&client_secret=<my client secret>&grant_type=client_credentials'
}
}).then(function(response) {
return response.json()
}).then(json => {
console.log(json)
})

关于javascript - 如何将 Active Directory token curl 命令转换为节点获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67512476/

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