Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用nodejs和node fetch从Azure for Microsoft Graph获取客户端令牌_Javascript_Node.js_Azure_Microsoft Graph Api - Fatal编程技术网

Javascript 如何使用nodejs和node fetch从Azure for Microsoft Graph获取客户端令牌

Javascript 如何使用nodejs和node fetch从Azure for Microsoft Graph获取客户端令牌,javascript,node.js,azure,microsoft-graph-api,Javascript,Node.js,Azure,Microsoft Graph Api,我一直在尝试遵循这个方向,并将其应用于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: <c

我一直在尝试遵循这个方向,并将其应用于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'
我应该注意,当我传递我的租户ID、客户端ID和密码时,这个CURL示例确实可以正常工作

我的
提取
正文有什么问题?

试试以下方法:

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)
    })
结果:

# 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='';
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)
    })