[英]How to get a client token from Azure for Microsoft Graph using nodejs and node-fetch
I've been trying to follow the direction here and applying it to nodejs.我一直在尝试遵循此处的方向并将其应用于 nodejs。
I'm getting the following error:我收到以下错误:
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'
This is my code.这是我的代码。
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)
})
What doesn't it like about my body that it doesn't see 'grant_type' in it?我的身体有什么不喜欢它没有看到“grant_type”的地方?
Another way to ask this question, might be, How to convert this curl command to a node-fetch
request using POST:问这个问题的另一种方法可能是,如何使用 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'
I should note that this CURL example does work work correctly when I pass my tenant ID, client ID and secret.我应该注意到,当我传递我的租户 ID、客户端 ID 和密码时,这个 CURL 示例确实可以正常工作。
What is wrong with my fetch
body?我的
fetch
身体有什么问题?
Try this:尝试这个:
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)
})
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.