gpt4 book ai didi

javascript - 如何使用 nodejs 和 node-fetch 从 Azure for Microsoft Graph 获取客户端 token

转载 作者:行者123 更新时间:2023-12-03 01:23:59 25 4
gpt4 key购买 nike

我一直在尝试遵循here的方向并将其应用到nodejs中。

我收到以下错误:

  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'

这是我的代码。

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)
})

我的 body 里没有看到“grant_type”,这有什么不舒服的地方吗?

提出这个问题的另一种方法可能是,如何使用 POST 将此curl命令转换为node-fetch请求:

# 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'

我应该注意到,当我传递租户 ID、客户端 ID 和 secret 时,此 CURL 示例确实可以正常工作。

我的 fetch 正文出了什么问题?

最佳答案

试试这个:

const fetch = require('node-fetch');

let tenantId='';
let clientID = '';
let clientSecret = '';

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=${clientID}&Client_secret=${clientSecret}&grant_type=client_credentials&scope=https://graph.microsoft.com/.default`
}).then(function(response) {
return response.json()
}).then(json => {
console.log(json)
})

结果:

enter image description here

关于javascript - 如何使用 nodejs 和 node-fetch 从 Azure for Microsoft Graph 获取客户端 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67511783/

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